Palindrome function fun

This is a discussion on Palindrome function fun within the C Programming forums, part of the General Programming Boards category; Hello programmers, My assignment is to create a function that checks whether or not the user inputted string is a ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Palindrome function fun

    Hello programmers,

    My assignment is to create a function that checks whether or not the user inputted string is a palindrome.

    I think I made a mistake in the palindrome function because it seems to be unable to check inputs with spaces in it. The error most likely lies in the section where I attempt to use the isalpha function.

    Here is my code, any help is very much appreciated!

    Code:
    /*------------------Main Functions------------------
    Purpose: Prompts user to input string
    Returns: Singal to the OS
    --------------------------------------------------*/
    
    int palindrome (char *string);
    
    int main(void) {
    
    char input[81];
    char *stop = "done";
    
    printf("Enter a string: ");
    gets(input);
    
    while( strcmp ( input, stop ) != 0) {
       if( palindrome ( input ) ) {
          printf("\nIt is a palindrome\n");
          }
       else {
          printf("\nIt is not a palindrome\n");
          }
       printf("\nEnter a string: ");
       gets(input);
       }
    printf("\n***** Program Terminated *****\n");
    }
    
    /*------------------Palindrome Functions------------------
    Purpose: Checks whether input is a palindrome
    Returns: TRUE or FALSE
    --------------------------------------------------*/
    
    int palindrome (char *string) {
    
    char *string_end = string + strlen(string) - 1;
    
    while(TRUE) {
       while( isalpha( *string ) ) {
          string ++;
          }
       while( isalpha( *string_end) ) {
          string--;
          }
       if( string_end <= string) {
          return TRUE;
          }
       if( tolower(*string) != tolower(*string_end)) {
          return FALSE;
          }
       string_end--;
       string++;
       }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is it you think isalpha does, and how do you think that's checking for matches?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    I was told isalpha checks whether or not something is a letter. I actually forgot to put the not operator in front of the two isalpha functions, which I have in my code.

    my while loop is actually this:

    Code:
    while(TRUE) {
       while( !isalpha( *string ) ) {
          string ++;
          }
       while( !isalpha( *string_end) ) {
          string--;
          }
       if( string_end <= string) {
          return TRUE;
          }
       if( tolower(*string) != tolower(*string_end)) {
          return FALSE;
          }
       string_end--;
       string++;
       }

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    I managed to fix my code.

    I was actually decrementing the wrong thing in my while loop. I also changed from !isalpha( ) to !isalnum( ). My while loop should have been:

    Code:
    while(TRUE) {
    
       while( !isalnum( *string ) ) //changed functions here{
    
          string ++;
    
          }
    
       while( !isalnum( *string_end) ) //and here {
    
          string_end --;  //fix here
    
          }
    
       if( string_end <= string) {
          return TRUE;
          }
       if( tolower(*string) != tolower(*string_end)) {
          return FALSE;
          }
       string_end--;
       string++;
       }

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    You need to be checking if the ends of the passed in string match as you move those end points towards each other. If you reach the middle of the string or the end points pass one another then you can return true. If at any point the end points do not match you can abort early and return false.
    "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. Palindrome Function
    By italia4fav in forum C Programming
    Replies: 4
    Last Post: 01-27-2011, 04:59 PM
  2. the palindrome
    By haochao in forum C Programming
    Replies: 15
    Last Post: 10-11-2008, 11:29 PM
  3. palindrome
    By nightingale in forum C Programming
    Replies: 2
    Last Post: 07-26-2003, 02:45 PM
  4. HELP W/ Palindrome function!!??
    By riley03 in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 06:47 PM
  5. palindrome function
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2002, 08:45 PM

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