I have this string
"go for goa"
and the output should be
"go for goa"
I want to remove the extra spaces. That means two or more consecutive spaces should be replaced with one space. I want to do it using an in place algorithm.
Below is the code I tried but it doesn't work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Function to remove spaces in an string array */
char *removeSpaces(char *str) {
int ip_ind = 1;
/* In place removal of duplicate spaces*/
while(*(str + ip_ind)) {
if ((*(str + ip_ind) == *(str + ip_ind - 1)) && (*(str + ip_ind)==' ')) {
*(str_ip_ind-1)= *(str + ip_ind);
}
ip_ind++;
}
/* After above step add end of string*/
*(str + ip_ind) = '';
return str;
}
/* Driver program to test removeSpaces */
int main() {
char str[] = "go for go";
printf("%s", removeSpaces(str));
getchar();
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…