Simple string question?

This is a discussion on Simple string question? within the C Programming forums, part of the General Programming Boards category; Any tips as to how to take a string input and return a copy of the string without the blanks?...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Simple string question?

    Any tips as to how to take a string input and return a copy of the string without the blanks?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Setup a char pointer to your string, and then use

    Code:
    isspace()
    to determine whether you are pointing at white space or not.

    Then either move the pointer along to the next element, or add the pointer value to another array depending on the result of isspace.

    Code:
    p = array;
    
    while ( *p != '\0' )
    {
    	if ( isspace(*p) )
    	{
    		++p;
    	}
    	else 
    	{
    		newarray[i++] = *p;
    		++p;
    	}
    }

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    how come this doesn't work for me?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXSIZE 80
    
    int main()
    {
        char array[MAXSIZE];
        int i = 0;
        int size;
    
        printf("Enter a string: ");
    
        fgets(array, MAXSIZE, stdin);
        size = sizeof(array);
        while(i < size)
        {
            if( isspace(array[i]) )
            {
                size--;
            }
            else
            {
               printf("%c", array[i]);
            }
            i++;
        }
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Quote Originally Posted by mapunk
    how come this doesn't work for me?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXSIZE 80
    
    int main()
    {
        char array[MAXSIZE];
        int i = 0;
        int size;
    
        printf("Enter a string: ");
    
        fgets(array, MAXSIZE, stdin);
        size = sizeof(array);
        while(i < size)
        {
            if( isspace(array[i]) )
            {
                size--;
            }
            else
            {
               printf("%c", array[i]);
            }
            i++;
        }
    
        return 0;
    }
    Works for me.

    Except the fact that your loop will not terminate when it hits the end of your string.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    Well im no expert

    dont listen to me im a noob
    Last edited by Mikecore; 12-03-2005 at 08:14 PM.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Quote Originally Posted by jamie85
    Works for me.

    Except the fact that your loop will not terminate when it hits the end of your string.
    Yeah, it took out all the spaces/tabs and what not, but it was getting a bunch of other random data from other parts of the memory (which is b/c its not terminating when it should) But, I thought thats what the size-- would do, b/c it decrements the size, so every time it takes a space out it should do one less loop of the while loop

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
       while ( array[i] )
       {
          if ( !isspace(array[i]) )
          {
             putchar(array[i]);
          }
          i++;
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Quote Originally Posted by Dave_Sinkula
    Code:
    while(array[i])
    lol, thanks...don't know why i didn't think of that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

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