# C Program to check whether a string is palindrome or not

Last updated on July 27, 2020

[no_toc]

## What is a Palindrome? #

A number or a word which remains the same, even after being reversed is called palindrome. For example, mom, radar, or the number 45654, all are palindrome.

The following is a C program to determine whether a string is a palindrome or not.

  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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 /******************************************************* Program to check whether a string is palindrome or not * * Enter a word: racecar * racecar is palindrome *******************************************************/ #include // include stdio.h library #include // include stdio.h library int main(void) { int len, i= 0, j, is_palindrome = 1; char word[50]; printf("Enter a word: "); scanf("%s", word); j = strlen(word) - 1; // get the last valid index while(i <= j) { if (word[i] != word[j]) { is_palindrome = 0; break; } i++; j--; } if(is_palindrome) { printf("%s is palindrome", word); } else { printf("%s is not palindrome", word); } return 0; } 

Expected Output: 1st run:

 1 2 Enter a word: racecar racecar is palindrome 

2nd run:

 1 2 Enter a word: netbeans netbeans is not palindrome 

Related Program: C Program to check whether the number is a Palindrome

## How it works #

The following table demonstrates what happens at each iteration of the while loop, assuming word = radar.

Iteration Condition i j
After 1st iteration word[0]!=word[1]=>'r'!='r'=>0 i=1 j=3
After 2nd iteration word[1]!=word[3]=>'a'!='a'=>0 i=2 j=2
After 3rd iteration word[2]!=word[2]=>'d'!='d'=>0 i=3 j=1

Hence, the string radar is palindrome.