Removing chars

This is a discussion on Removing chars within the C++ Programming forums, part of the General Programming Boards category; How can I remove parts of an array? Code: char message[60]; cin.get (message, 60); I want to remove everything except ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    63

    Removing chars

    How can I remove parts of an array?

    Code:
    char message[60];
    cin.get (message, 60);
    I want to remove everything except letters from the array.

    For example: THIS SITE & THAT SITE RULE!! would become THISSITETHATSITERULE

    Optimally, the deleted text would allow the rest of the array to bump down (if message[0] was whitespace it would remove it and replace things to make message[0] the next letter character).

    I did a google search but couldn't seem to find anything to do the trick for me.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    you can create a for loop that searches each array index for a " " whitespace character. If it is a " " whitespace then just insert "" nothing into that array slot.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A loop, and some of the nice functions from <cstring> ought to do the trick.
    http://www.cppreference.com/stdstring.html
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    And... You can't really insert a 'nothing' into the array slot. What you could do, is look for the next valid character, and move it down into that slot. You will end up with some empty space at the end of your string, so you could just recopy the string into another, appropriately size string. (Also, consider using std::string; it'd make your life a bit easier.)
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    After a few initial troubles (and a Simpson's episode later), I came up with this:

    Code:
    int y=0;
    char input[60];
    cin.getline (input, 60, '\n');   
    
    char message[strlen(input)];
    
    for(int i=0; i<(strlen(input)); i++) {
        input[i] = toupper(input[i]);
        
        if ( isalpha(input[i]) ) {
            message[y] = input[i];
            y++;     
        }    
    }
    Edit: I had a problem due to the array ending up shorter, so I put in code to put in a null value. Solved my own problem
    Last edited by Philandrew; 10-20-2004 at 09:08 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't allowing room for the null character in your message buffer.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    Quote Originally Posted by quzah
    You aren't allowing room for the null character in your message buffer.

    Quzah.
    Just found that before hitting refresh. Thanks though!

  8. #8
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    write it to a file then read it back in ingoring all the invalid characters would be the easiest IMO. That gets rid of the spaces.
    use strcat to add the different words together in the case of !@#$%^&*()[][]-=+_><.,\|/?~` just make a switch statement that ignores them and saves the good stuff.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    That would require creating a file, though, and would be a waste of space.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 05:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 01:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 01:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 03:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 05:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21