【发布时间】:2021-02-23 17:00:09
【问题描述】:
这个程序检查输入的字符串是否是回文。如果有空格或任何特殊字符,它应该以某种方式甚至应该告诉字符串是回文
像 messi 一样是 iss em 的回文
而 ronald!o 是 odlanor 的回文
这是程序,但由于某些奇怪的原因,它运行不正常
#include <stdio.h>
#include <string.h>
int main() {
char palstr[100], ans[100];
printf("enter the string for checking weather the string is a palindrome or not");
scanf("%[^/n]", &palstr);
int ispalin = 1, i = 0, n = 0;
int num = strlen(palstr);
printf("the total length of the string is %d", num);
while (i <= num) {
if (palstr[i] == ' ' || palstr[i] == ',' || palstr[i] == '.' ||
palstr[i] == '!' || palstr[i] == '?') {
i++;
}
palstr[n++] == palstr[i++];
}
int j = num;
i = 0;
while (i <= num) {
ans[j--] = palstr[i];
}
printf("the reverse of the string %s is %s", palstr, ans);
if (ans == palstr)
printf("the string is a palindrome");
else
printf("the string is not a palindrome");
return 0;
}
【问题讨论】:
-
您的程序卡住了,因为
i没有在while (i<=num)循环中更新。您的程序中还有一些错误。 -
哦,编辑又加错了……
-
提示:使用
for而不是while用于固定长度的循环。 -
⟼请记住,尤其是在 Stack Overflow 上学习和提问时,让您的代码尽可能地井井有条,这一点很重要。 Consistent indentation 有助于传达结构,更重要的是传达意图,这有助于我们快速找到问题的根源,而无需花费大量时间来尝试解码正在发生的事情。
标签: c