【问题标题】:palindrome c program is not working for some reason回文 c 程序由于某种原因无法正常工作
【发布时间】: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&lt;=num) 循环中更新。您的程序中还有一些错误。
  • 哦,编辑又加错了……
  • 提示:使用for 而不是while 用于固定长度的循环。
  • ⟼请记住,尤其是在 Stack Overflow 上学习和提问时,让您的代码尽可能地井井有条,这一点很重要。 Consistent indentation 有助于传达结构,更重要的是传达意图,这有助于我们快速找到问题的根源,而无需花费大量时间来尝试解码正在发生的事情。

标签: c


【解决方案1】:

需要考虑的几点。首先,关于代码:

if (ans == palstr)

不是你在C中比较字符串的方式,它比较字符串的地址,在这种情况下它们总是不同的。

比较字符串的正确方法是:

if (strcmp(ans, palstr) == 0)

其次,您应该在删除所有不需要的字符之后计算出字符串的长度,因为这是您将要使用的长度。我的意思是这样的:

char *src = palstr, dst = palstr;

while (*src != '\0') {
    if (*c != ' ' && *src != ',' && *src != '.' && *src != '!' && *src != '?') {
        *dst++ = *src;
    }
    src++;
}

第三,你的while 循环中有一个错误,如果你得到两个连续的坏字符,你只会删除第一个(因为你的if 这样做然后盲目地复制下一个字符无论如何)。


第四,您可能想考虑只去除所有非字母字符,而不是那一小部分:

#include <ctype.h>
if (! isalpha(*src) {
    *dst++ = *src;
}

第五个也是最后一个,你真的需要创建一个新字符串来检查回文(尽管如果你想反向打印字符串,你可能仍然需要),你可以只需从两端开始并向内移动,例如:

char *left = &palstr, right = palstr + strlen(palstr) - 1, ispalin = 1;
while (left < right) {
    if (*left++ != *right--) {
        ispalin = 0;
        break;
    }
}

我可能还错过了其他一些事情,但这应该足够开始了。

【讨论】:

  • 嘿,我做到了。但我仍然没有运气。我认为有很多错误。谢谢你的时间\
  • @GratusRichard:我列出了更详尽的清单,列出了您应该研究的内容。
  • 也可以看看scanf
【解决方案2】:

好吧,这段代码中的错误太多了。我会用 cmets 指出它们。

#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\n");

        scanf("%s", palstr); // your former code won't stop input util Ctrl+D

        int ispalin = 1, i = 0, n = 0;

        int num = strlen(palstr);

        printf("the total length of the string is %d\n", num);
        while (i < num) {  // < insted of <=
                if (palstr[i] == ' ' || palstr[i] == ',' || palstr[i] == '.' ||
                                palstr[i] == '!' || palstr[i] == '?') {
                        i++;
                        continue;// without this, marks still in the string
                }
                palstr[n++] = palstr[i++]; //should be =
        }
        palstr[n] = '\0'; //

        num = n; // the length might be changed

        i = 0;
        int j = num-1; // reverse
        while (i < num) { //
                ans[i++] = palstr[j--]; //
        }
        ans[i] = '\0'; //

        printf("the reverse of the string %s is %s\n", palstr, ans);
        //if (ans == palstr) they can never be equal
        if (strcmp(ans, palstr)==0)
                printf("the string is a palindrome\n");
        else
                printf("the string is not a palindrome\n");

        return 0;
}

【讨论】:

  • 在您的“已修复”scanf%s 上只需快速说明一下,将不允许输入空格。
  • 没错。如果确实需要空间,那么 get 可能会更好。
猜你喜欢
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
相关资源
最近更新 更多