简体   繁体   English

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

[英]to find if a given string is palindrome or is not palindrome

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!尝试在您的程序中输入“HelloHello”,它会说这是一个回文!

You need to compare these two:你需要比较这两个:

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

(and fix the braces, as suggested in the other answer) (并按照其他答案中的建议修复大括号)

To give you a clue I've done some tidier indenting of a bit of your code:为了给你一个线索,我对你的一些代码做了一些更整洁的缩进:

for(i=0;i<halflen;i++)
    {
        if(str[i]!=str[i+halflen])
            flag=0;
        break;
    }

You can also use STL to check if a given string is palindrome using function equal .您还可以使用 STL 使用函数equal检查给定字符串是否为回文。 Lets say you have an std::string named x , then the following function call determines if x is palindrome假设您有一个名为xstd::string ,那么以下函数调用确定 x 是否为回文

equal(x.begin(), x.begin() + x.size() / 2, x.rbegin());

Here you want something like...在这里你想要像......

    if(str[i]!=str[strlen (str) - i - 1])
    {
        flag = 0;
        break;
    }

The break needs to go in the if block otherwise it will always get executed. break需要进入if块,否则它总是会被执行。 Initialising flag at some point would be a good idea, too.在某些时候初始化flag也是一个好主意。 If I might be permitted an observation, ALWAYS enclose the if-block and else block in curly brackets even if there is only one statement;如果允许我进行观察,即使只有一个语句,也始终将 if-block 和 else 块括在大括号中; it would save you several of the problems you've got here.它可以为您省去您在这里遇到的几个问题。

Later - edited per Mr Rodriguez' comments below.后来 - 根据罗德里格斯先生在下面的评论进行编辑。

From the 2005 version of myself :我自己2005 版本

bool isAlphaNumeric(char c)
{
    return (iswalpha(c) || iswdigit(c));
}

bool isPalindrome(char *str)
{
    /* A man, a plan, Anal Panama!!! */
    if(*str == '\0')
    {
        return false;
    }

    int len = strlen(str);
    if(len <= 1) return true;

    char *start = str;
    char *end = start + len - 1;

    while(start < end)
    {
        if(!isAlphaNumeric(*start))
        {
            *start++;
            continue;
        }
        if(!isAlphaNumeric(*end))
        {
            *end--;
            continue;
        }
        if(towlower(*start) != towlower(*end))
        {
            return false;
        }
        *start++;
        *end--;
    }
    return true;
}
bool isPalindrome(char* str) {
    char* s = str;
    char* e = str;
    while(*e) e++;
    --e;
    while(s < e) {
        if(*s != *e) return false;
        ++s; --e;
    }
    return true;
}

Here's a shorter solution (C++; identical line count for C):这是一个较短的解决方案(C++;C 的行数相同):

bool is_p(char const * const str, ptrdiff_t n)
{
  if (n < 1) return false;

  auto p = str, q = str + n - 1;
  while (*(p++) == *(q--))
    if (p >= q)
      return true;
  return false;
}

A C -flavored solution : )一个C味的解决方案 :)

bool is_palindrome(const char* s) {
    const char* p = s;
    while (*p != '\0') ++p;
    while (s < p) if (*s++ != *--p) return false;
    return true;
}
#include <stdio.h>
#include <string.h>
int main()
{
    char a[100], b[100];
    printf("Enter a string to check if it is a palindrome\n");
    gets(a);
    strcpy(b, a); 
    strrev(b); 
    if (strcmp(a, b) == 0) 
        printf("The string is a palindrome.\n");
    else
        printf("The string isn't a palindrome.\n");
    return 0;
}

Here is a better way.这里有一个更好的方法。

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
    string input;
    cout << "Enter your text: ";
    cin >> input;
    transform(input.begin(), input.end(), input.begin(), ::tolower);
    if (input[0] == input[input.length()-1])
        cout << "Palindrome";
    else
        cout << "not palinrome";
    cin.ignore();
    cin.get();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM