3

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;
}
cdg53
  • 95
  • 1
  • 2
  • 5
  • 3
    You want to use strlen not sizeof – Vaughn Cato Mar 28 '13 at 04:09
  • You are doing it the wrong way. You are comparing only the first characters of your string. You will need to compare all the characters. Also `sizeof` returns the size of the types, use `strlen`. Hint: use another loop. – MD Sayem Ahmed Mar 28 '13 at 04:10
  • is it considered cheating to use strrev and strcmp? – danh Mar 28 '13 at 04:16
  • 2
    Don't use gets(). Ever. – Randy Howard Mar 28 '13 at 04:16
  • what is the problem with using gets() over scanf()? – cdg53 Mar 28 '13 at 04:24
  • 1
    @user2023041, `gets` has no overflow protection at all. Taken from cppreference docs on it: *The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.* – chris Mar 28 '13 at 04:29
  • @user2023041, [This](http://c-faq.com/stdio/getsvsfgets.html) is the problem. – Anish Ramaswamy Mar 28 '13 at 04:30

2 Answers2

4

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.

danh
  • 62,181
  • 10
  • 95
  • 136
1

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71
  • I can break it right now, simply because it still uses gets(), and you can't prevent it. – Randy Howard Mar 28 '13 at 04:28
  • I don't think logically, there is any wrong with using gets. @user2023041: There is no problem with gets.only problem is its not safe and fgets are always suggested instead of gets – Ritesh Kumar Gupta Mar 28 '13 at 04:32
  • -1. I always think answers should explain _why_ things are wrong. Also if possible and appropriate (like in this case, it is), explain _what_ happens because of those mistakes. This way the OP can understand the consequences and learn from mistakes. – Anish Ramaswamy Mar 28 '13 at 05:01
  • @AnishRam: What? What??????? Didn't i explain, where and what are the mistakes? Come On. Think twice before downvote.No one in this world will downvote above answer and that too after it got accepted by the poster – Ritesh Kumar Gupta Mar 28 '13 at 05:03
  • 1
    @ritesh_nitw, Whoops! I guess I got confused by your answer. I take that back haha. Unfortunately, my downvote is locked until you edit your answer. – Anish Ramaswamy Mar 28 '13 at 05:35