找到一个给定的字符串是回文或不是回文 [英] to find if a given string is palindrome or is not palindrome

查看:131
本文介绍了找到一个给定的字符串是回文或不是回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个程序,找出如果输入的字符串是回文与否回文但它总是说,它不是一个回文

 的#include<&CONIO.H GT;
#包括LT&;&graphics.h中GT;
#包括LT&;&string.h中GT;
无效的主要(无效)
{
    INT I,LEN,halflen,标志= 1;
    焦炭海峡[50];
    clrscr();
    的printf(请输入一个字符串:\\ n);
    得到(STR);
    LEN = strlen的(STR);
    halflen = LEN / 2;
    对于(i = 0; I< halflen;我++)
    {
        如果(STR [I]!= str中第[i + halflen])
            标志= 0;
        打破;    }
    如果(标志)
        的printf(这是一个回文。);
    其他
        的printf(这不是一个回文。);
    残培();
}


解决方案

您比较核心是有缺陷的:

 如果(STR [I]!= str中第[i + halflen])

这是不是比较这两个角色,你认为它是。

尝试输入HelloHello到你的程序,它会说这是一个回文!

您需要比较两个:

 如果(STR [I]!= STR [LEN-I-1])

(和固定支架,所建议的其他答案)

I made a program to find if a entered string is palindrome or not palindrome but it always says that its not a palindrome

#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();
}

解决方案

Your central comparison is flawed:

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

This isn't comparing the two characters you think it is.

Try entering "HelloHello" into your program, it will say it is a palindrome!

You need to compare these two:

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

(and fix the braces, as suggested in the other answer)

这篇关于找到一个给定的字符串是回文或不是回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆