c++ How to extract the whitespace between words if there is one

1.6k Views Asked by At

I've got two questions. I need to write a program that extracts all non-alphabetic characters and displays them, then removes them.

I am using isalpha which is working for symbols, but only if the input string has no spaces like "hello world"

but if it is more than one word like "hello! world!", it will only extract the first exclamation mark but not the second.

Second question which may be related, I want my program to detect the spaces between the words (I tried isspace but I must have used it wrong? and remove them and put them in a char variable

so for example if the input is hello4 world! How3 are you today? I want it to tell me

removed: 4 removed: removed: ! removed: removed: 3 removed: removed: removed:

long story short, if there is no other way, I'd like to detect spaces as !isalpha, or find something similar to isalpha for space between text.

Thanks

# include <iostream>
# include <string>

using namespace std;

void main()
{
    string message;


    cin >> message;

    for (int i = 0; message[i]; i++)

    if(!isalpha(message[i]))


        cout << "deleted following character: " << message[i] <<endl;
        else
        cout <<"All is good! \n";


   }
2

There are 2 best solutions below

0
On BEST ANSWER

>> reads a single word, stopping when a whitespace character is found. To read a whole line, you want

std::getline(cout, message);
3
On

There is a better way by which you can get non-alphabetic characters,

You can check with asci value of each character and compare with alphabetic asci character if not in it & not a space (space asci val), then you get your non-alphabetic character. You can get all ascii codes over here :=> http://www.asciitable.com/

-Jayesh