write a program to check whether the entered string is polindrome or not
Answers
Answer:
C Program To Check A String Is Palindrome Or Not | C Programs
C program to check whether a string is a palindrome or not – In this article, we will detail in on the multiple ways to check whether a string is a palindrome or not in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
C Program Find Circumference Of A Circle | 3 Ways
C Program Area Of Trapezium – 3 Ways | C Programs
Also check list of over 500+ C Programs with source codes.
The ways used in this piece are as follows:
Using Standard Method
Using Function
Using Recursion
Using String Library Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
C Program To Check A String Is Palindrome
As you can see in the image uploaded above, you need to enter the concerned string first.
The string entered here is ‘alula‘.
As it is clearly visible, ‘alula’ is a palindrome.
Thus, to check the same in C programming is as follows:
Using Standard Method
If the original string is equal to reverse of that string, then the string is said to be a palindrome.
2)Read the entered string using gets(s).
3) Calculate the string length using string library function strlen(s) and store the length into the variable n.
4) i=0,c=0.
Compare the element at s[i] with the element at s[n-i-1].If both are equal then increase the c value.
Compare the remaining characters by increasing i value up to i<n/2.
5) If the number of characters compared is equal to the number of characters matched then the given string is the palindrome.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int i,n,c=0;
printf("Enter the string : ");
gets(s);
n=strlen(s);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}
Output:
1
2
3
Enter the string: WELCOME
before reverse = WELCOME
after reverse = EMOCLEW