removing a character from a char array

This is a discussion on removing a character from a char array within the C++ Programming forums, part of the General Programming Boards category; hi, I was wondering how I could look through a char array and remove any white space characters I found. ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    9

    removing a character from a char array

    hi,

    I was wondering how I could look through a char array and remove any white space characters I found. eg if I a had a char array of

    char buffer[] = "hell o";

    I could modify it to "hello";

    can anyone help

    Thank you

    Craig Smith

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    char a[] = "Hell o",
         b[10] = {0};
    for ( int x = 0, y = 0; a[x] != '\0'; x++ )
      if ( !isspace ( a[x] ) b[y++] = a[x];
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Didn't you ask this already?

    Well to do it in a more practical sense, why not just monitor what is put in the array (whether it is through user input or whatever):
    Code:
    while( (c = cin.get()) != '\n')
    {
       switch( c )
       {
          case ' ':       // If it is a space
             break;
          default:
             array[i++] = c;
             break;
        }
    }
    Another way is to use the isspace() function in ctype.h with an if-else tree.

    If you want to just go through an array and delete spaces, it is just searching and shifting. I think you should be able to figure out the code for it. Here' s the pseudo anyway:

    for i = 0 to ArrayLength
    if you find a space then shift everything after it up one

    Good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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