Hello,
Im hoping someone can help me. Im trying to write a program to determine if a word or phrase is palindrome or not, the same letters forward as backwards (bob, level, madam im adam). No matter what my keyboard input is, it always sets my "palindrome" flag to "1". I even added lines to verify my array was being read correctly. Even if the characters displayed are not the same, my if statement goes as true and sets the flag high. I have modified and tested it many times, and Im sure the problem is the If statement that compares the individual characters. Im sure it is something I have messed up, but I cant seem to find what it is. The code is pasted below. Any help would be much appreciated.
Code:
// project created on 11/18/2009 at 4:20 PM
#include <stdio.h>
#include <string.h>
main()
{
{
//The Following Lines Are Declarations And Initializations //
int x, first, last, palindrome = 1;
char str[100];
//The Following Lines Are User Input Statements//
printf("Enter A Word Or Phrase To Check Whether It Is A Palindrome Or Not- ");
printf("No Spaces Or Punctuation Please. ");
scanf("%s",str);
//The Following Lines Are Declarations and Initializations//
first = 0;
x = strlen(str);
last = x - 1;
//The Following Two Lines Were Added To Assist Me In Debugging//
//This Proves The Values Were Read Correctly From The Array//
printf ("The Beginning First Character Is %c. ",str[first]);
printf ("The Beginning Last Character Is %c. ",str[last]);
//The Following While Loop Sets The Loop For The String Length//
while (first <= last)
{
//The Following If/Else If Statement Evaluates Individual Characters In The str String//
if (str[last] == str[first])
first = (first + 1),last = (last - 1);
else
palindrome = 0, last = 0;
}
//The Following If/Else Statement Takes The Result From The Character Evaluation And Displays The Result//
if(palindrome = 0)
printf("This Is Not A Palindrome");
else if(palindrome = 1)
printf("This Is A Palindrome");
}
}