Palindrome identifierrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr rrrrrrrrrrrrrrrrrrrrrrrr

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ovserj1994
    New Member
    • Feb 2015
    • 3

    Palindrome identifierrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr rrrrrrrrrrrrrrrrrrrrrrrr

    Hello, This is my code. I need to create a program where the user can enter a phrase (up to 100 characters), and the program will check if it is a Palindrome. I wrote the code and it works fine with 1 exception. It tells me that phrase is not a palindrome If I type: "Bob bob" "Lemon nomel", where first letter is upper case and last letter is lower case.DOnt know what to do. thats the code. Heeeeeeelp!


    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define TRUE  1
    #define FALSE 0
    
    
    void make_copy_of_string(char str[], char str_copy[]);          //copies the string
    void keep_chars(char string[]);                                 //reads nothing but letters
    void convert_upper_to_lower(char string[]);                     //covert upper case to lower
    _Bool palindromeness(char string[]);
    
    
    int main (void)
    {
            char phrase[101], phrase_copy[101];
    
            printf("Enter a Phrase for palindrome checking: ");
            fgets(phrase, 101, stdin);                                      //reads first 100 letters
    
            make_copy_of_string(phrase, phrase_copy);                       //makes copy of the phrase
            keep_chars(phrase_copy);                                        //keeps only letters
            convert_upper_to_lower(phrase_copy);                            //convert
    
    
            if(palindromeness(phrase_copy) == TRUE )
               printf("Your phrase is: %s\nAnd it is a palindrome!\n", phrase);
    
            else
               printf("Your phrase is not a palindrome!\n", phrase);
    
            return 0;
    }
    
    
            void make_copy_of_string(char str[], char str_copy[])           //make_copy_of_string function
    
    {
               int i=0;
    
            while (str[i] != '\n' && str[i] != '\0')
    {
               str_copy[i] = str[i];
               i++;
    }
               str_copy[i] = '\0';
               str[i]      = '\0';
    
    }
    
            void  keep_chars(char string[])                         //keep_chars function
    {
               int  i=0, j=0;
    
            while (string[i] != '\0')
    {
    
            if(  ('A'<=string[i] && string[i] <= 'Z') || ('a'<=string[i] && string[i] <= 'z') )
    {
              string[j] = string[i];
              i++;
              j++;
    }
            else
    {
              i++;
    }
    
    }
    
            string[j] = '\0';                                       //add terminating NULL
    
    }
    
           void convert_upper_to_lower(char string[])               //convert_upper_to_lower function
    {
    
            int i;
    
            if ( 'A'<=string[i] && string[i]<='Z');
    {          i+32;
    
    }
    }
    
            _Bool palindromeness (char str[])
    {
            char string[101];
    
            strcpy(string,str);
    
               int i, j;
               char temp[100];
               for(i=strlen(str)-1, j=0; i+1!=0; --i,++j)
    {
            temp[j]=str[i];
    }
            temp[j]='\0';
            strcpy(str,temp);
    
            if( strcmp(string, str)== 0)
              return TRUE;
            else
              return FALSE;
     }
    Last edited by Rabbit; Feb 12 '15, 01:02 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Take a look at isalpha(), toupper(), and tolower() in ctype.h.

    The C Standard allows use of character encodings other than ASCII. Some non-ASCII character encodings do not assign contiguous values to the letters of the alphabet. Thus, tests like those on lines 58 and 80 are not portable. Use isalpha() instead.

    Non-ASCII character encodings may have different relationships between the values of upper- and lowercase characters. Thus, conversions like the one at line 81 are not portable. Use toupper() or tolower() instead. These standard conversion functions return the input value if it isn't one that needs to be converted, so you don't need to test the values yourself as you did on line 80.

    You strip all non letters from the string before testing if it is a palindrome. Maybe you only want to strip white space from the string ... If you want "Bob4..4bob " to be considered a palindrome.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This code:
      Code:
             void convert_upper_to_lower(char string[])               //convert_upper_to_lower function
      {
      
              int i;
      
             [B] if ( 'A'<=string[i] && string[i]<='Z');
      {          i+32;
      
      [/B]}
      The semi-colon at the end of the if terminates the if-statement without doing anything. Also, i+32 doesn't do anything either.

      Worse, the int i is used without an initial value.

      Comment

      Working...