# Remove Spaces?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-20-2005
98dodgeneondohc
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"?
• 04-20-2005
itsme86
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.
• 04-20-2005
joshdick
That algorithm is quadratic, but it can be done in linear time. Think about it a bit more.
• 04-20-2005
Dave_Sinkula
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.
• 04-20-2005
Jaken Veina
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;  }```
• 04-20-2005
quzah
Quote:

Originally Posted by Jaken Veina
scanf(&input[index]);

What pray tell is this supposed to do?

Quzah.
• 04-20-2005
Prelude
>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. ;)
• 04-20-2005
Pressure
Code:

```#include <stdio.h> #include <ctype.h> int main(void){         int c;         while ((c=getchar()) != EOF){                 if( !isspace(c))                         putchar(c);         } }```
• 04-21-2005
rmps

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; }```
• 04-21-2005
98dodgeneondohc
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?
• 04-21-2005
quzah
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.
• 04-21-2005
98dodgeneondohc
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?
• 04-21-2005
joshdick
Code:

```an_array[i] = c; ++i;```
Put that in the loop, but make sure you have enough room in your array.
• 04-21-2005
98dodgeneondohc
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??
• 04-21-2005
Dave_Sinkula
>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 */```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last