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;
}
Comment