Remove Spaces?

This is a discussion on Remove Spaces? within the C Programming forums, part of the General Programming Boards category; How do I remove spaces from a sentence enter by the user? say I have an array: char sentence[40]; and ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Remove Spaces?

    How do I remove spaces from a sentence enter by the user?

    say I have an array:

    char sentence[40];

    and the user inputs a value into sentence... say the user enters: "hello world world world"

    how do I take that input, and convert it to "helloworldworldworld"?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    One way would be to loop through the array and every time you encounter a space, move all the contents after the space to their previous index. For example, if sentence[5] is a space, then move sentence[6] to sentence[5], sentence[7] to sentence[6], etc.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    That algorithm is quadratic, but it can be done in linear time. Think about it a bit more.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by 98dodgeneondohc
    How do I remove spaces from a sentence enter by the user?
    I wonder if anyone has asked this question before? Maybe try a quick search.
    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.*

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Do you need to remove the spaces and then do something to the remaining sentence, or are you simply trying to remove the spaces and print the line back out. If you are, you could simply set up a loop that goes through the each element of the char array one by one and prints it if it isn't a space.

    Otherwise, you could remove the spaces when you read in the line. Like so.
    Code:
    int index = 0
    char* input;
    input = malloc(1);
    
    while(index == 0)
     {
      scanf(&input[index]);
      if(input[index] == ' ')
        continue;
      else if(input[index] == '\n')
        break;
      input = realloc(input, index + 1);
      ++index;
     }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Jaken Veina
    scanf(&input[index]);
    What pray tell is this supposed to do?

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

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Otherwise, you could remove the spaces when you read in the line. Like so.
    It's a good thing you tested this to make sure it worked, otherwise we might think that your funny looking code was wrong.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void){
    	int c;
    
    	while ((c=getchar()) != EOF){
    		if( !isspace(c))
    			putchar(c);
    	}
    }

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    How about something like:

    Code:
    /**
     * remstrchr - Remove all occurrences of character "c"
     *	       from string "string". Returns the modified
     * 	       string.
     * @string: Input string (must be != NULL).
     * @c: Character to be removed.
     */
    char *
    remstrchr(char *const string, const char c)
    {
    	char *src;
    	char *dst;
    
    
    	for (src = dst = string; *src != '\0'; src++)
    		if (*src != c)
    			*dst++ = *src;
    	*dst = '\0';
    
    	return string;
    }
    "In My Egotistical Opinion, most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    I don't quite understand the codes you guys gave.

    This is the program that I am working on:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
          char sentence[50];
          int count;
          int a;
          int b;
    
          printf("Enter A Sentence, The Computer Will Tell You Whether The Sentence Is A Palindrome Or Not!:\n");
          fgets(sentence,sizeof sentence, stdin);
    
             for(count=0;sentence[count]!='\0';count++);
             for(a=count-2,b=0;a>=0;b++,a--)
                {
                   if(toupper(sentence[b])!=toupper(sentence[a]))
                      {
                         printf("This Is Not A Palindrome!");
                         getchar();
                         return 0;
                       }
                 }
    
    printf("This Is A Palindrome!");
    getchar();
    
    
    return 0;
    }
    It's a program that tells the user is a word entered is a palindrome or not. For you thats not familiar with a palindrome, its a word(s) that are the same when they are read forward or backwards. ex: straw warts, racecar, damn i agassi miss again mad ...etc...

    It works fine for words like: straw warts

    but it does not work for something like: race car

    both words are palindrome, but it will not detect "race car" as palindrome unless the word is entered without a space (racecar).

    How would I modify it so the program will automatically remove all spaces from the entered words?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always just copy all the characters that aren't spaces into a new buffer and use that, if you can't understand the other ways to do it. (Did you search the forum for removing spaces or some such as suggested?)

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

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by Pressure
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void){
    	int c;
    
    	while ((c=getchar()) != EOF){
    		if( !isspace(c))
    			putchar(c);
    	}
    }

    this is the loop that I want to use. How would I make it so that instead of each character being printed out in the screen, the computer stores it in an array?

  13. #13
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    an_array[i] = c;
    ++i;
    Put that in the loop, but make sure you have enough room in your array.

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Quote Originally Posted by joshdick
    Code:
    an_array[i] = c;
    ++i;
    Put that in the loop, but make sure you have enough room in your array.
    what does the "i" stand for??

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >what does the "i" stand for??

    It is an index into the array.

    Rather than "removing all spaces" -- because you might also want to remove punctuation or control characters -- aren't you really trying to keep only letters?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       char sentence[40] = "Damn, I Agassi.\tMiss again mad.";
       int i, j = 0;
    
       printf("sentence = \"%s\"\n", sentence);
       for ( i = 0; sentence[i] != '\0'; ++i )
       {
          if ( isalpha(sentence[i]) )
          {
             sentence[j++] = tolower(sentence[i]);
          }
       }
       sentence[j] = '\0';
       printf("sentence = \"%s\"\n", sentence);
    
       for ( i = 0, --j; i < j; ++i, --j )
       {
          if ( sentence[i] != sentence[j] )
          {
             puts("Not a palindrome");
             return 0;
          }
       }
       puts("Looks like a palindrome to me");
       return 0;
    }
    
    /* my output
    sentence = "Damn, I Agassi.	Miss again mad."
    sentence = "damniagassimissagainmad"
    Looks like a palindrome to me
    */
    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.*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help remove spaces + punctuation
    By spchehe in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2010, 11:07 AM
  2. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  3. How to remove end spaces from string
    By nitinmhetre in forum C Programming
    Replies: 4
    Last Post: 12-16-2006, 01:14 AM
  4. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  5. Replies: 5
    Last Post: 06-30-2003, 01:52 PM

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