加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 查找给定的字符串是回文还是不是回文

发布时间:2020-12-16 06:47:37 所属栏目:百科 来源:网络整理
导读:我做了一个程序来查找输入的字符串是否是回文序列,但它总是说它不是回文结构 #include conio.h #include graphics.h #include string.hvoid main(void){ int i,len,halflen,flag=1; char str[50]; clrscr(); printf("Enter a string:n"); gets(str); len=st
我做了一个程序来查找输入的字符串是否是回文序列,但它总是说它不是回文结构

#include <conio.h> 
#include <graphics.h> 
#include <string.h>
void main(void)
{
    int i,len,halflen,flag=1;
    char str[50];
    clrscr();
    printf("Enter a string:n");
    gets(str);
    len=strlen(str);
    halflen=len/2;
    for(i=0;i<halflen;i++)
    {
        if(str[i]!=str[i+halflen])
            flag=0;
        break;

    }
    if(flag)
        printf("It is a Palindrome.");
    else
        printf("It is not a Palindrome.");
    getch();
}

解决方法

你的中心比较是有缺陷的:

if (str[i] != str[i+halflen])

这不是比较你认为的两个字符.

尝试在你的程序中输入“HelloHello”,它会说它是一个回文!

你需要比较这两个:

if (str[i] != str[len-i-1])

(并修复大括号,如另一个答案所示)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读