Is it a Palindrome?

This is a discussion on Is it a Palindrome? within the C Programming forums, part of the General Programming Boards category; Hi, I'm writing a program that will identify whether or not the user's input string is a palindrome or not. ...

  1. #1
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    Is it a Palindrome?

    Hi, I'm writing a program that will identify whether or not the user's input string is a palindrome or not. However, the program keeps the inputed string is a palindrome eventhough it is not. It will say that the string is not a palindrome if the inputed string is a little bit longer. Please, help me!

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 50
    int palindrome(char[]);
    
    int main()
    {
      char str[MAX];
      printf("Enter a string : ");
      scanf("%s",str);
      if(palindrome(str)==1)
      {
        printf("\nThe string is palindrome.\n");
      }
      else
      {
        printf("\nThe string is not palindrome.\n");
      }
      return 0;
    }
    
    int palindrome(char str[])
    {
      int flag = 0;
      int start, end;
      char rev[MAX];
            // Create another string for checking.
      for(start=0;str[start]!='\0';start++)
      {
        rev[start] = str[start];
      }
            // Check whether the string is palindrome.
            // Using the user's input to check with the new string.
      for(start=0, end=strlen(str)-1; start<=end; start++, end--)
      {
        if(str[start] == rev[end])
        {
          flag = 1;
          return(flag);
        }
      }
      flag = 0;
      return (flag);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Your function doesn't test the whole string - look where you return.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    xp5
    xp5 is offline
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    WOW! thank you, i didn't see that!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    There's no need for you to create a duplicate string internally to compare against the input argument. You aren't even creating a properly terminated string (you stop copying characters right before you get to the NULL) even though it doesn't matter that much in this case since you are using strlen on the original string and not this copy.

    Just do something like this:
    Code:
    int palindrome(char str[])
    {
        int flag = 1;
        int start, end;
    
        for(start=0, end=strlen(str)-1; flag && start<end; start++, end--)
        {
            if(str[start] != str[end])
                flag = 0;
        }
    
        return flag;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 11:36 AM
  2. Palindrome Program using Memory mapping
    By rrsanch in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2004, 11:49 PM
  3. bool palindrome definition
    By justinc911 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 04:50 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:05 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21