I am trying to write a code to remove all the leading, trailing and middle of the sentence spaces but to keep only one space between the words.
For example if the input is
" This is my string "
the output should be
"This is my string"
So far I have come up with this:
#include <stdio.h>
void cascade(char str[], int i);
main()
{
char str[100] = " This is my string ";
int i = 0;
while (str[i] != '\0') {
if (str[i] == ' ' && str[i + 1] == ' ')
cascade(str, i);
i++;
}
printf("%s\n", str);
}
void cascade(char str[], int i)
{
while(str[i] != '\0') {
str[i] = str[i + 1];
i++;
}
str[i + 1] = '\0';
}
I would appreciate if anyone could come up with some ideas, Thank you.
1 Answer