简体   繁体   中英

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!

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 . Lets say you have an std::string named x , then the following function call determines if x is palindrome

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. Initialising flag at some point would be a good idea, too. 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; 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 :

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):

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 : )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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