DEV Community

Cover image for Writing a Program for Palindrome in C
Gaurav Chaudhary
Gaurav Chaudhary

Posted on

Writing a Program for Palindrome in C

Program for Palindrome in C Understanding, Palindromes have been a source of fascination for centuries and have been used in literature, language, and computing since ancient times.

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as “madam” or “racecar”. In this article, we will discuss palindromes in C programming and how to use them in your programs.

Introduction to Palindrome
Palindromes are words, phrases, numbers, or other sequences of characters that can be read the same forward and backward. Common examples of palindromes include “madam” and “racecar”. Palindromes have been used in literature, language, and computing since ancient times.

Palindrome in C Programming
Palindromes can be used in C Programming to check if a string is a palindrome.

To do this, you can use the strcmp() function to compare the string to its reverse. If the strings are the same, then the string is a palindrome.

Also.. Check Out Other Tutorials

Writing a Program for Palindrome in C
You can also create a palindrome program in C. To do this, you can use the strrev() function to reverse a string and then compare it to the original string. If the strings match, then the program outputs “This is a palindrome”.

#include<stdio.h>
#include<string.h>

&nbsp;

int main()

{

char str[20];

int l,i;

int flag=0;

printf("Enter a string of length less than 20\n");

scanf("%s",str);

&nbsp;

l=strlen(str);

&nbsp;

for(i=0;i<l;i++)

{

if(str[i]!=str[l-i-1])

{

flag=1;

break;

}

}

if(flag==1)

{

printf("%s is not a palindrome\n",str);

}

else

{

printf("%s is a palindrome\n",str);

}

return 0;

}
Enter fullscreen mode Exit fullscreen mode

Conclusion
In this article, we discussed palindromes in C programming and how to use them in your programs. Palindromes can be used to check if a string is a palindrome and to create a palindrome program in C. By using palindromes in C programming, you can add an interesting and unique element to your programs.
Source: AnyCoder

Top comments (0)