Write a program using string function that determines if the input word is a
palindrome. A palindrome is a word that produces the same word when it is
reversed.
Enter a word: AHA
Reversed word: AHA
It is a palindrome.
#include <stdio.h>
#include <string.h>
int isPalindrome(char inputString[]);
int main()
{
char inputString[40];
//get a string from the user
printf("Enter a string: ");
scanf("%s",inputString);
//display result
if(isPalindrome(inputString)==1){
printf("The word %s is a palindrome", inputString);
}else{
printf("The word %s is NOT a palindrome", inputString);
}
getchar();
getchar();
return 0;
}
//Checks if word is a palindrome
int isPalindrome(char inputString[]){
int index = 0;
int length = strlen(inputString) - 1;
while (length > index)
{
if (inputString[index] != inputString[length])
{
return 0;
}
index++;
length--;
}
return 1;
}
Comments
Leave a comment