cdg53
cdg53

Reputation: 95

Determine if input is a palindrome in C

I'm fairly new to C and I'm having a little bit of a problem. I have to write a program that takes user input and determines if the input is a palindrome. I've got the program to reverse the input, but I'm having trouble getting the strings to compare. All input comes out as not being a palindrome. I'm suppose to use integer subscript index to be able to compare the input. Also I'm suppose to ignore all non letter characters, which I think is a c.type function.

#include <stdio.h>
#include <string.h>
#define N 50

main()
{
    char array[N] = {0};
    char front;
    char end;
    char x;
    char w =0;
    char i;
    char forward;
    char reverse;

    printf("Enter Message: ");
    gets(array);

    front = sizeof(array);
    end = sizeof(array) - 1; 

    for( i = 0; i <= front; i++){      
       forward = array[i];

    } 
    for( x = end; x >= 0; x--){
       reverse = array[x];
    }

    if (forward != reverse){
       w = 1;
    }

    if(w == 1){
      printf("Not a Palindrome");
    } 
    else{
        printf("Palindrome");
    }

    printf("\n");
    return 0;
}

Upvotes: 3

Views: 4018

Answers (2)

Ritesh Kumar Gupta
Ritesh Kumar Gupta

Reputation: 5191

There were many errors in your code.

Firstly reverse and forward should be of type char[] , not char. use strlen to find the length of string. sizeof won't work. sizeof(array) will always return (sizeof(char)*50)

There was an error in reversing string. It should be reverse[i]=array[x].

For comparing whether original string and reverse string are equal or not,you were only comparing one character since variables reverse and forward are of char type in your code. You should compare original(forward) and reverse string(char array) using strcmp function.

I have corrected it.This should works fine for every test cases.

#include <stdio.h>
#include <string.h>
#define N 50
int main()
{
    char array[N] = {0};
    char front;
    char end;
    char x;
    char i;
    char forward[N];
    char reverse[N];

    printf("Enter Message: ");
    gets(array);

    front = strlen(array);
    end = strlen(array) - 1; 
    for( i = 0; i <= front; i++)
    {      
        forward[i]= array[i];

    } 
    for( i=0,x = end; x >= 0; i++,x--)
    {
        reverse[i]= array[x];
    }
    reverse[i]=0;
    if (strcmp(forward,reverse)!=0)
    {
        printf("Not a Palindrome");
    } 
    else{
        printf("Palindrome");
    }

    printf("\n");
    return 0;
}

CHECK DEMO at IDEONE

Upvotes: 1

danh
danh

Reputation: 62686

My C is a little rusty, but you only need to search at most half the length of the string (minus 1).

int isPalindrome(char *str)
{
      char *p1 = str
      char *p2 = str + strlen(str) - 1;

      while(p2 > p1) {
          if (*p1 != *p2) return 0;
          p1++; p2--;
      }
      return 1;
}

We could work the auto-increment and decrement into the equality check, but it's a little tougher to read.

Upvotes: 4

Related Questions