A String and Digits is Palindrome
Palindrome is a word, phrase, number, or sequence of characters that remains the same when read backward. In this tutorial, we will learn how to determine if a given string or digit is a palindrome or not using C programming language.
To check if a string is a palindrome, we need to reverse the string and compare it with the original string. If both the original and reversed strings are equal, then the string is a palindrome. Similarly, to check if a digit is a palindrome, we need to reverse the digit and compare it with the original digit. If both the original and reversed digits are equal, then the digit is a palindrome.
We can use loops and if-else statements in the palindrome program to check whether the given string or digit is a palindrome or not. By using these constructs, we can compare the original string or digit with its reverse and determine whether they are equal or not.
In this tutorial, we will discuss various methods of checking whether a given string or digit is a palindrome or not using C language. We will also provide a sample code for each method, along with an explanation of how it works. By the end of this tutorial, readers will have a clear understanding of how to check if a string or digit is a palindrome using C programming language.
Source Code:
- A string is Palindrome or not
#include <stdio.h>
#include <string.h>
void main()
{
char str[50], temp;
int i, lelf, right, len;
printf (" \n Enter string: ");
scanf( "%s", &str);
len = strlen(str);
left = 0;
right = len - 1;
while(right>lelf){
if(str[lelf++]==str[right--]){
printf("%s is a palindrome",str);
return;
}
}
printf("%s is not a palindrome",str);
}
Output:
Enter a String: mom
mom is a palindrome
Output:
Enter a String: Hey
Hey is not a palindrome
You can use this program to find a given digit is palindrome or not.
Output:
Enter Digit: 232
232 is a palindrome
Output:
Enter Digit: 234
234 is not a palindrome
- Digit is Palindrome or not
#include <stdio.h>
#include <string.h>
void main()
{
int num, digit, reverse=0;
printf("Enter Digits: ");
scanf("%d",&num);
int temp=num;
while(num>0){
digit=num%10;
reverse=(reverse*10)+digit;
num=num/10;
}
if(temp==reverse)
printf("%d is palindrome",num);
else
printf("%d is not palindrome",num);
}
Output:
Enter Digit: 232
232 is palindrome
Output:
Enter Digit: 234
234 is not palindrome
0 Comments