在C中用单个空格替换多个空格
我想用单个空格在字符串中重复多个空格,但是我的下面的代码不起作用。 什么是逻辑错误?
#include #include main() { char input[100]; int i,j,n,z=0; scanf("%d",&n); z=n; for(i=0;i<n;i++) scanf("%c",&input[i]); for(i=0;i<n;i++) { if(input[i]==' ' && (input[i+1]==' ' || input[i-1]==' ')) { --z; for(j=i;j<n;j++) input[j]=input[j+1]; } } for(i=0;i<z;i++) printf("%c",input[i]); printf("\n"); }
scanf
给你一些问题:它在输入长度n
后读取你给出的\n
n
。 因此,您将错过自循环退出以来的最后一个字符。 已经给出的答案已经足够好了。 但是如果你想遵循自己的逻辑,试试这个:
void main() { char input[100]; int i = 0,j,n = 0; while ((input[n] = getchar()) != '\n') { n++; } input[n] = '\0'; while (i < n) { if(input[i]==' ' && (input[i+1]==' ' || input[i-1]==' ')) { for(j=i;j
我会做这样的事情:
void replace_multi_space_with_single_space(char *str) { char *dest = str; /* Destination to copy to */ /* While we're not at the end of the string, loop... */ while (*str != '\0') { /* Loop while the current character is a space, AND the next * character is a space */ while (*str == ' ' && *(str + 1) == ' ') str++; /* Just skip to next character */ /* Copy from the "source" string to the "destination" string, * while advancing to the next character in both */ *dest++ = *str++; } /* Make sure the string is properly terminated */ *dest = '\0'; }
当然,上面的函数要求您正确终止字符串,这是您目前没有的。
上面的函数是什么,基本上是将字符串复制到自身上。 例外情况是当存在空间时,简单地丢弃多个空格。
由于函数修改了源字符串,因此不能在字符串文字中使用它。
if(input[i]==' ' && (input[i+1]==' ' || input[i-1]==' '))
case“1 3”:当i == 0时输入[i-1] Out-of-Bounds
scanf("%d",&n);
保持换行符,(输入[0] < - '\ n')
修复
scanf("%d%*c",&n);
#include char* uniq_spc(char* str){ char *from, *to; int spc=0; to=from=str; while(1){ if(spc && *from == ' ' && to[-1] == ' ') ++from; else { spc = (*from==' ')? 1 : 0; *to++ = *from++; if(!to[-1])break; } } return str; } int main(){ char input[]= " abc de f "; printf("\"%s\"\n", uniq_spc(input));//output:" abc de f " return 0; }
你不能尝试这个简单的代码:
#include #define IN 1 #define OUT 0 int main() { int c, spaces, state; spaces = 0; state = OUT; while ((c = getchar()) != EOF) { if ( c == ' ') { ++spaces; state = OUT; } else if (state == OUT) { state = IN; spaces = 0; } if (c == ' ' && spaces > 1 && state == OUT) c = 0; putchar(c); } return 0; }
您必须修复以下for循环。 你的for循环的限制应该是z
而不是n
for(j=i;j
通过
for(j=i;j
BTW :你的scanf()
(读取字符串)获得的拳头字符是换行符( \n
)。 这个换行符来自第一个scanf()
十进制( %d
)
为什么要让它变得比它需要的更复杂? 您可以使用strtok
检查单个空格,然后忽略它们。 然后你可以使用strcat
将字符串连接成一个完整的句子,然后你就完成了。
这就是我做的方式:
#include #include #include #include int main(void) { char *pch; char sentence[1000]; char without[1000]; printf("Sentence: "); fgets(sentence,1000, stdin); strtok(sentence, "\n"); // remove any newlines pch = strtok(sentence, " "); while(pch != NULL) { strcat(without, pch); strcat(without, " \0"); pch = strtok(NULL, " "); } without[strlen(without)-1] = '\0'; // remove extra whitespace at the end printf("|%s|\n",without); return 0; }
#include #include int main(void) { char input[1000]; int i=0; gets(input); for(i=0;input[i]!='\0';i++) { if(input[i]!=' ' || input[i+1]!=' ') printf("%c",input[i]); } return 0; }
#include #include void remove_blanks(char* s); int main() { char const s[] = {'1',' ',' ','2',' ',' ','3'}; remove_blanks(s); printf("%s",s); return 0; } void remove_blanks(char* s){ int i=0, delta=0, cnt=0; for (i=0;s[i];++i){ if (s[i]==' ') cnt++; if (cnt>1){ delta+=1; cnt=0; } s[i-delta]=s[i]; if(delta>0) s[i]='\0'; } }