从C中的字符串中删除多余的空格

我有这个字符串

"go for goa" 

输出应该是

 "go for goa" 

我想删除多余的空格。 这意味着应该用一个空格替换两个或多个连续的空格。 我想使用就地算法来做到这一点。

下面是我尝试的代码,但它不起作用:

 #include  #include  #include  /* 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) = '\0'; return str; } /* Driver program to test removeSpaces */ int main() { char str[] = "go for go"; printf("%s", removeSpaces(str)); getchar(); return 0; } 

大多数解决方案似乎不必要地复杂化

 #include  #include  void strip_extra_spaces(char* str) { int i, x; for(i=x=0; str[i]; ++i) if(!isspace(str[i]) || (i > 0 && !isspace(str[i-1]))) str[x++] = str[i]; str[x] = '\0'; } int main(int argc, char* argv[]) { char str[] = " If you gaze into the abyss, the abyss gazes also into you. "; strip_extra_spaces(str); printf("%s\n",str); return 0; } 

我甚至不知道你的function是怎么做的。 首先,第一次通过,-1将在字符串开始之前访问。 尝试以下方法:

 char *removeSpaces(char *str) { char *inp = str, *outp = str; int prevSpace = 0; while (*inp) { if (isspace(*inp)) { if (!prevSpace) { *outp++ = ' '; prevSpace = 1; } } else { *outp++ = *inp; prevSpace = 0; } ++inp; } *outp = '\0'; return str; } 

你没有检查空间..你正在检查一个标签。 替换\ t(我的意思是空间..)

你的if条件不起作用。 我会告诉你我的代码。 它类似于你的,只需使用两个指针: backfront

如果front不是空格,或front是空格但back不是空格,则需要front复制back+1

 char *removeSpaces(char *str) { if (*str == '\0') return str; char *back = str; char *front = str + 1; /* In place removal of duplicate spaces*/ while(*front != '\0') { if (*front != ' ' || *back != ' ') // highlight *(++back) = *front; front++; } /* After above step add end of string*/ *(back + 1) = '\0'; return str; } 

希望这可以帮到你。

你去:

 #include  #include  #include  /* Function to remove spaces in an string array */ char *removeSpaces(char *str) { int ip_ind = 0; while (*(str+ip_ind) != 0) { if (*(str + ip_ind++) == 32) { if (*(str + ip_ind) == 32) { int x=ip_ind; while (*(str + x +1) != 0) { *(str + x)= *(str + 1 + x++); } *(str + x)= 0; --ip_ind; } } } return str; } /* Driver program to test removeSpaces */ int main() { char str[] = "go for go"; printf("%s\n", str); printf("%s\n", removeSpaces(str)); char str2[] = "go for go for go"; printf("%s\n", str2); printf("%s\n", removeSpaces(str2)); return 0; } 

输出:

 dda$ ./a.out go for go go for go go for go for go go for go for go dda$ 

问题:

  1. 您正在跟踪’\ t’标签,但您必须删除这些空格。
  2. 此外,您正在跟踪并找到’\ t’, 但您没有删除它的步骤(我添加了一个)。
  3. 每次你不能增加ip_ind ,你必须inc,只有当没有删除时,因为删除完成后,该场景将等于增加。
  4. 您的方案将因""而失败,为了避免它,添加如下所示的检查参数(方法1),或者从0开始从ip_ind开始,如(方法2)。 (感谢@Lee Daniel Crocker)

解:

你可以这样试试。,

方法1:ip_ind从一个开始。

 /* Function to remove spaces in an string array */ char *removeSpaces(char *str) { int ip_ind = 1; char *ptr; if(*str) return str; /* In place removal of duplicate spaces*/ while(*(str + ip_ind)) { if ( (*(str + ip_ind) == *(str + ip_ind - 1)) && (*(str + ip_ind)==' ') ) { ptr = str + ip_ind; //Functionality for removal of spaces. do{ *(ptr-1) = *ptr; }while(*ptr++ != '\0'); } else //Inc only if deletion is not done. ip_ind++; } /* After above step add end of string*/ *(str + ip_ind) = '\0'; return str; } 

方法2:ip_ind从零开始。

 char *removeSpaces(char *str) { int ip_ind = 0; char *ptr; /* In place removal of duplicate spaces*/ while(*(str + ip_ind)) { if ( (*(str + ip_ind) == *(str + ip_ind + 1)) && (*(str + ip_ind)==' ') ) { ptr = str + ip_ind+1; do{ *(ptr-1) = *ptr; }while(*ptr++ != '\0'); } else ip_ind++; } /* After above step add end of string*/ *(str + ip_ind) = '\0'; return str; } 

简单的一个给你

 #include  #include  #include  int main() { char a[100]; int i,t,k; scanf("%[^\n]s",a); t=strlen(a); for(i=0;i