-2

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";


   }
Paralytic
  • 5
  • 3
  • 2
    Get cracking with writing the code for your homework (i noticed that you have not been a member for long and do not understand that we do not write code for free) – Ed Heal Nov 01 '14 at 07:34
  • I don't want anyone to write my code for free :) I want to tell me why isalpha is not detecting the symbols in the second or third word, just the first. I am indeed a new member, and also very new to programming and self teaching atm. I excercise by solving problems I found online and I am stuck on this one atm :) Thanks though. PS. I only wrote what I want as output to explain that I want it to tell me when it removed a space as well. not to have you write it :) – Paralytic Nov 01 '14 at 07:36
  • Why not put you code here - the reason why it is not working as expected could be a multitude of reasons – Ed Heal Nov 01 '14 at 07:38
  • I am attempting to do so now..trying to figure out how :P – Paralytic Nov 01 '14 at 07:46
  • So show us your attempt (and please make sure it compiles) – Ed Heal Nov 01 '14 at 07:47
  • There it is. How if i were to write one1 two2 three3 it will only detect 1 not the spaces or the 2 or the 3. Why is that? – Paralytic Nov 01 '14 at 07:48
  • Please tidy the code up (so somebody can cut'n'paste it easily). Also bung in the braces PS: As it is C++ it should be `int main` – Ed Heal Nov 01 '14 at 07:50
  • Done..I'm sorry I'm new to all this :P – Paralytic Nov 01 '14 at 07:53
  • @Paralytic As you're already using `isalpha()`, note there's `isspace()` to check for whitespace characters (including TAB and `'\n'`). – πάντα ῥεῖ Nov 01 '14 at 08:07

2 Answers2

0

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

  • Thank you! I actually did not think of this at all. – Paralytic Nov 01 '14 at 07:53
  • I'm still wondering though, why did the `isalpha()` method not work? Does the for-loop terminate on spaces? – Sanchises Nov 01 '14 at 09:09
  • it seems like it yes. I'm still having trouble figuring this out but I'm trying! isalpha does get the symbols etc, but not the spaces. and it ends after the first word. – Paralytic Nov 01 '14 at 09:17
0

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

std::getline(cout, message);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644