Question about taking off parts of a word

Discussion in 'Mac Programming' started by Beckie, May 1, 2005.

  1. macrumors newbie

    Joined:
    Apr 6, 2005
    #1
    Hi everyone
    I have to getline(cin, string)
    Then I have to output the string by separating the words and making them "valid words."

    Like for instance I need to change the word To: to the word to
    and hat@edu.gov!!! to hat@edu.gov Then I'm supposed to put it into an array.

    I think I can do the array part, but I don't know how to output the list of words without the extra characters that don't belong since the list could be an infinite amount of possibilities.

    Thanks in advance for the help
    Take care,
    Beckie
     
  2. macrumors 6502a

    Joined:
    Apr 19, 2005
    #2

    ok so you need to remove all invalid characters?

    ok.. let's give this a little perspective and see if you can figure it out.

    there's a bunch of functions in the ctype.h file..

    int isdigit( int c) returns true if c is a digit and false otherwise
    int isalpha ( int c ) returns true if c is a letter and false otherwise

    since i'm not sure exactly what the correct list of "acceptable" characters is i'll give you a very generic way of doing this.

    i'd have 2 strings. 1 with the original, and the other with an altered one you'll be building from scratch.

    string 1 ... start with string[0] .. cast as an int.. this will give you an integer .. oddly enough it'll give you the ASCII character number for that letter.

    uppercase letters are A-Z starting at 65-90 so 65 = A, 90 = Z
    lowercase letters are a-z starting at 97-122 so a = 97, z = 122

    so... walk through each character of the string and compare your stuff..

    for ( int x = 0; x <= strlen(string); x++ ) {

    if ( dynamic_cast< int >(string[x]) >= 65 &&
    dynamic_cast< int >(string[x]) <= 90 ||
    dynamic_cast< int >(string[x]) >= 97 &&
    dynamic_cast< int >(string[x]) <= 122 ) {

    do what you need to do to copy the character to your other string

    }

    }

    what this does is remove ALL characters that are not A-Z or a-z

    look up the ASCII codes for all others you would want as well and throw them in there.
     
  3. thread starter macrumors newbie

    Joined:
    Apr 6, 2005
    #3
    Thank you so much for your help!
    Take care,
    Beckie
     

Share This Page