gpt4 book ai didi

c - C中的字符串回文

转载 作者:行者123 更新时间:2023-11-30 21:25:19 27 4
gpt4 key购买 nike

我正在编写一个字符串回文程序,代码编译成功,但在运行时接受字符串,但此后什么也没有,输出窗口保持不动,光标闪烁,请帮我看看这段代码有什么问题。我正在使用 dev-c++

    gets(ch); // the program stops here
p=ch;

while(ch!='\0')
{ p++;
size++;
}


for(i=0;i<20;i++)
{
if(c[i]==c[j])
printf("string is pallindrome");

else printf("string is not pallindrome");
}

getch();
return 0;
}

最佳答案

问题是这样的:

    while(ch!='\0')

ch 是一个 char 数组,您将其与单个 char 进行比较。

此外,size 未初始化。

我建议这样:

size=0;
while(ch[size]!='\0')
{ p++;
size++;
}

或者,使用指针方法:

 while(*p!=0)
{
p++;
size++;
}

此外,不要在 for 循环 中打印(这会使其打印多次),而是使用标志变量。

关于c - C中的字符串回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31010008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com