How to check String is Palindrome in C Language?

The Test Coder
3 min readFeb 5


A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. In this article, we’ll discuss how to check if a string is a palindrome in the C programming language.

Below we are giving you an example of code that checks if a string is a palindrome or not. After the code we will explain how its working.

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

int main()
char str[100];
int length, i, flag = 0;
printf("Enter a string: ");
scanf("%s", str);
length = strlen(str);
for (i = 0; i < length; i++)
if (str[i] != str[length - i - 1])
flag = 1;
if (flag)
printf("%s is not a palindrome.", str);
printf("%s is a palindrome.", str);
return 0;

This code first reads the string from the user and then calculates its length. The main logic of the program is implemented in the for loop, which compares the first and last characters, second and second-last characters, and so on. If any of these characters are not equal, then the flag is set to 1 and the loop is broken.

After the loop, the value of the flag is checked. If it’s 1, the string is not a palindrome and a corresponding message is printed. If it’s 0, the string is a palindrome and a corresponding message is printed.

In this example, we’ve used the scanf function to read the string from the user. If you want to read the string including spaces, you can use the gets function or the fgets function. The gets function is not recommended for use as it is vulnerable to buffer overflow attacks.

Some Facts About Palindrome

  1. Palindromes are often used in word play and puzzles. For example, “A man, a plan, a canal, Panama!” is a famous palindrome.
  2. Palindromes can be found in many different forms, including numbers, words, phrases, and sequences of characters.
  3. Palindromes have been used for centuries in poetry, literature, and philosophy, and they continue to be a popular subject in modern literature and media.
  4. Palindromes are not limited to the English language. They can be found in many different languages, including Greek, Latin, and Hebrew.
  5. The concept of a palindrome is closely related to symmetry. A palindrome is symmetrical about its center, meaning that if you split it in half, the two halves are mirror images of each other.
  6. The longest known palindrome is a 13,874-word sentence in Finnish, which is a Guinness World Record.
  7. Palindromes have many applications in computer science, including string matching, cryptography, and compression.
  8. Palindromes have been studied extensively in mathematics and computer science, and there are many algorithms for checking if a given string is a palindrome.
  9. Palindromes can also be found in DNA sequences, where they play an important role in the regulation of gene expression and the stability of the genome.
  10. Palindromes can be used to create interesting and challenging puzzles, such as palindrome puzzles, where the goal is to create a palindrome from a given set of letters or numbers.

In this article, we discussed how to check if a string is a palindrome in C. This simple example demonstrates the basic approach to checking palindromes in C. You can extend this example to handle other types of inputs and edge cases as per your requirements.