"A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, with general allowances for adjustments to punctuation and word dividers." - Wikipedia
This is a C program to check if a given string is a palindrome or not.
Written by Munia Balayil
This is a C program to check if a given string is a palindrome or not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A palindrome is a word, phrase, number or other sequence | |
* of units that can be read the same way in either direction | |
**/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
int main(void) | |
{ | |
int i, j, l; | |
char str1[20], str2[20]; | |
printf("Enter any word:"); | |
scanf("%s", str1); | |
/* Find the length of the string */ | |
l = strlen(str1); | |
j = 0; | |
/* Store the reversed string in another string str2 */ | |
for(i = l - 1; i >= 0; i-- ) | |
str2[j++] = str1[i]; | |
str2[j] = '\0'; | |
/* Compare str1 and str2 */ | |
/* strcmp returns 0 when the strings are equal, */ | |
/* a negative integer when s1 is less than s2, */ | |
/* or a positive integer if s1 is greater than s2, */ | |
/* according to the lexicographical order. */ | |
if(strcmp(str1, str2) == 0) | |
printf("The word is a palindrome\n"); | |
else | |
printf("The word is not a palindrome\n"); | |
return 0; | |
} |
No comments:
Post a Comment