c - 反向字符串比较回文

问题描述

我想检查输入的文本是否有回文。

但是,当我输入回文时,我总是知道它不是回文。我是否对存储的换行符做错了什么?

#include<stdio.h>
#include<string.h>

int main()
{
char text[256],reverse[256];
int i,j;

    printf("Type a text: ");
    fgets(text,255,stdin);

    j = strlen(text)-1;
    
    for (i=0; i<=j; ++i)
        {
            if(text[i] >= 'A' && text[i] <= 'Z')
            {
            text[i] += 32;
            }
            if(text[i] == '\n')
            {
            text[i] = i - 1;
            }
        }

    i = strlen(text)-1;

    for (i = i,j = 0 ;i >= 0 ; --i,++j)
        {
        reverse[j] = text[i];
        }

    printf("Text: %s\n",text);
    printf("Reverse: %s\n",reverse);

    if (strcmp(text,reverse) == 0)
        {
        printf("The entered text \"%s\" is a palindrome!\n",text);
        }
    else
        {
        printf("The entered text \"%s\" is NOT a palindrome!\n",text);
        }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)