Write a program that replaces two or more consecutive blanks in a string by a single
blank. For example, if the input is Grim return to the planet of apes!!
the output should be Grim return to the planet of apes!!.
#include <stdio.h>
#include <ctype.h>
char* remove_spaces(char* str) {
char* p = str;
char* q = str;
int has_space = 0;
while (*p) {
if (isspace(*p)) {
if (!has_space) {
*q = *p;
q++;
has_space = 1;
}
}
else {
*q = *p;
q++;
has_space = 0;
}
p++;
}
*q = '\0';
return str;
}
int main()
{
char buf[1024];
printf("Enter a string: ");
gets(buf);
remove_spaces(buf);
printf("Clean string is:\n##%s##\n", buf);
return 0;
}
Comments
Leave a comment