1. ## 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. 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.

3. That algorithm is quadratic, but it can be done in linear time. Think about it a bit more.

4. 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.

5. 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. Originally Posted by Jaken Veina
scanf(&input[index]);
What pray tell is this supposed to do?

Quzah.

7. >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.

8. Code:
```#include <stdio.h>
#include <ctype.h>

int main(void){
int c;

while ((c=getchar()) != EOF){
if( !isspace(c))
putchar(c);
}
}```

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;
}```

10. 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. 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.

12. 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. Code:
```an_array[i] = c;
++i;```
Put that in the loop, but make sure you have enough room in your array.

14. 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. >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."