UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to check whether a string is palindrome or not

C program to check the given string is palindrome or not

by Krishna viswambharan


Posted on 28 Sep 2017 Category: C Views: 896

Edited on 24 Jan 2018


C program to check whether a string is palindrome or not

In this article, we will see how to check whether a string is a palindrome or not. Below C program code read a string and check if it is a palindrome, without using library functions.

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

void main()
{
   char string[25], reverse[25];
   int  i, length = 0, flag = 0;
 
   fflush(stdin);
   printf("Enter a string \n");
   gets(string);
   for (i = 0; string[i] != '\0'; i++)
   {
       length++;  //compute the length of the input string
   }
   for (i = length - 1; i >= 0; i--)
   {
      reverse[length - i - 1] = string[i];  //reverse input string
   }
   for (i = 0; i < length; i++)
   {
       if (reverse[i]==string[i]) //comparing reversed string and input string
           flag = 1;
       else
           flag = 0;
   }
   if (flag == 1)
       printf("%s is  palindrome \n", string);
   else
       printf("%s is not  palindrome \n", string);
}

output



Leave a Comment:


Advertisement