C 截断给定字符串中的空格
输入-C 截断给定字符串中的空格,c,C,输入-Hello World 输出-HelloWorld 这是我用c写的程序 但我有错 我使用的逻辑是,当我找到一个空格时,我将其与下一个字符交换,直到结束,然后插入一个“\0”字符 #include <stdio.h> int main() { char s[12]="Hello World"; char *t; t=s; while(*t) {
Hello World
输出-HelloWorld
这是我用c写的程序
但我有错
我使用的逻辑是,当我找到一个空格时,我将其与下一个字符交换,直到结束,然后插入一个“\0”字符
#include <stdio.h>
int main()
{
char s[12]="Hello World";
char *t;
t=s;
while(*t)
{
char *p;
p=t;
if(*p==' ')
{
while(*p !='\0')
{
char x;
x=*p;
*p=*(p+1);
*(p+1)=x;
p++;
}
*(p-1)='\0';
}
t++;
}
printf("%s\n",s);
}
#包括
int main()
{
char s[12]=“你好,世界”;
char*t;
t=s;
while(*t)
{
char*p;
p=t;
如果(*p=='')
{
而(*p!='\0')
{
字符x;
x=*p;
*p=*(p+1);
*(p+1)=x;
p++;
}
*(p-1)='\0';
}
t++;
}
printf(“%s\n”,s);
}
通过调用此函数来交换讨厌的嵌套while循环
void siftLeftAtCurrentPos(char* cstr)
{
while(*cstr)
{
*cstr = *(cstr + 1);
cstr++;
}
}
然后不要增加
t
,直到*p!=''代码>通过调用此函数来交换嵌套的while循环
void siftLeftAtCurrentPos(char* cstr)
{
while(*cstr)
{
*cstr = *(cstr + 1);
cstr++;
}
}
然后不要增加t
,直到*p!=''代码>只需取出:
这就是问题所在。只需取出:
这就是问题所在。K&R风格复制:
#include <stdio.h>
int main()
{
char s[12]="Hello World";
char *src, *dst;
for(src=dst=s; *dst = *src; src++) {
if( *dst == ' ') continue;
dst++;
}
printf("%s\n",s);
return 0;
}
#包括
int main()
{
char s[12]=“你好,世界”;
字符*src,*dst;
对于(src=dst=s;*dst=*src;src++){
如果(*dst='')继续;
dst++;
}
printf(“%s\n”,s);
返回0;
}
K&R样式副本:
#include <stdio.h>
int main()
{
char s[12]="Hello World";
char *src, *dst;
for(src=dst=s; *dst = *src; src++) {
if( *dst == ' ') continue;
dst++;
}
printf("%s\n",s);
return 0;
}
#包括
int main()
{
char s[12]=“你好,世界”;
字符*src,*dst;
对于(src=dst=s;*dst=*src;src++){
如果(*dst='')继续;
dst++;
}
printf(“%s\n”,s);
返回0;
}
您的内部while循环是一个无限循环。交换最后生成的空格时,下一个字符也将是空格
正如Jonathan在回答中提到的,您可以通过向左移动而不是交换值来解决这个问题。这就是说,您可以制作一个更高效的算法,在一次传递中删除空间,而无需嵌套循环。如果您有一个充满空格的字符串,那么当前的算法将需要二次时间
char* in = s; //next character to read;
char* out = s; //where to write the next non-space character;
//copy all the non spaces
while(*in){
if(*in != ' '){
*out = *in;
out++;
}
in++;
}
//now fill the rest of the strings with null values:
while(out != in){
*out = '\0';
out++;
}
您的内部while循环是一个无限循环。交换最后生成的空格时,下一个字符也将是空格
正如Jonathan在回答中提到的,您可以通过向左移动而不是交换值来解决这个问题。这就是说,您可以制作一个更高效的算法,在一次传递中删除空间,而无需嵌套循环。如果您有一个充满空格的字符串,那么当前的算法将需要二次时间
char* in = s; //next character to read;
char* out = s; //where to write the next non-space character;
//copy all the non spaces
while(*in){
if(*in != ' '){
*out = *in;
out++;
}
in++;
}
//now fill the rest of the strings with null values:
while(out != in){
*out = '\0';
out++;
}
现在在while循环中,我写了*p=*(p+1);p++;非常感谢…它工作了…但是早期发生的事情…为什么它会显示错误…但是我使用x作为用于交换的字符变量…@user2712068当您将(p+1)*设置为x时,您在最后一次传递时丢失了NULL,这使得循环无限。@user2712068更不用说您在过程中重写了调用堆栈的事实了。。。lol.now在while循环中,我写了*p=*(p+1);p++;非常感谢…它工作了…但是早期发生的事情…为什么它会显示错误…但是我使用x作为用于交换的字符变量…@user2712068当您将(p+1)*设置为x时,您在最后一次传递时丢失了NULL,这使得循环无限。@user2712068更不用说您在过程中重写了调用堆栈的事实了。。。哈哈。为什么会这样……如果我在那个时候这么做,会发生什么呢?你们已经知道那个字符会被覆盖。当你把p+1复制到p时,它变得多余了。为什么会这样。如果我这样做的话,实际上会发生什么,你已经知道这个字符将被覆盖。当你将p+1复制到p+1时,它变得额外了,因为这是一个会让OOP类型发疯的解决方案。好吧,它只是K&R strcpy()的一个变体(当然可以避免继续;它只用于教育目的;-)@Jim,因为OOP类型会阅读关于C的问题?lol+1因为这是一个会让OOP类型发疯的解决方案。好吧,它只是K&R strcpy()的一个变体(当然可以避免continue;它只是出于教育目的;-)@Jim因为OOP类型会阅读关于C的问题?英雄联盟