string pallendrom program in c
Answers
C program to check if a string or a number is palindrome or not. A palindrome string is one that reads the same backward as well as forward. It can be of odd or even length. A palindrome number is a number that is equal to its reverse. Our program works as follows: at first, we copy input string into a new string (strcpy function), and then we reverse (strrev function) it and compare it with the original string (strcmp function). If both of them have the same sequence of characters, i.e., they are identical, then the string is a palindrome otherwise not. C program to check palindrome without using string functions. Some palindrome strings are: "C", "CC", "madam", "ab121ba", "C++&&++C".
Palindrome program in C language
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter a string to check if it's a palindrome\n");
gets(a);
strcpy(b, a); // Copying input string
strrev(b); // Reversing the string
if (strcmp(a, b) == 0) // Comparing input string with the reverse string
printf("The string is a palindrome.\n");
else
printf("The string isn't a palindrome.\n");
return 0;
}
Answer:#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
// reversed integer is stored in reversed variable
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if orignal and reversed are equal
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
Explanation: