I am new to c++ programming and would like to write a program which has the following requirement:
Given a text consists of
- words
- letters
- numbers
- punctuations, and
- whitespaces.
Filter out any characters that not in the range of 0..9
, a..z
or A..Z
.
This means that when I typed in:
The quick brown fox jumps over the lazy dog!
The output will be:
Thequickbrownfoxjumpsoverthelazydog
I have typed the following codes and try to run it and the outcome is fine. However, when I submitted it onto another c++ platform for checking the validity, there is no output to be generated.
I am so confused... Please help if you could. Thank you very much to you all.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line;
getline(cin, line);
for (int i = 0; i < line.size(); ++i)
{
if (!((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || (line[i] >= '0' && line[i] <= '9')))
{
line[i] = '\0';
}
}
cout << line;
return 0;
}
\0
is the end of a string, so when you use this, you are cutting off your string at the first occurence.You'd better remove that char from your array, but then I'd advise you to go from the end back to the beginning:
Pseudo-code: