Palindrome in C without using String.h functions

  • 13 June 2016
  • Mohd Shibli
  • C-programming

«MALAYALAM» well it is a language spoken in the southern region of India but do you notice something special about it…..Nope, now reverse «MALAYALAM» and see….yeah you got it the reverse of MALAYALAM is exactly the same of its original word, this is known as a palindrome.

Palindrome in C without using string.h

If a word or number, when reversed, is exactly equal to its original word or number is known as palindrome number or palindrome string for example 121, NITIN, MALAYALAM etc.
In this tutorial we will make a C program to check whether the user entered string is palindrome or not.
Before writing the code we will first understand its logic.

Also read: C Program to convert Seconds into Hours, Minutes and Seconds

Palindrome logic:

Let’s take a simple palindrome string for example ‘NITIN’ now what we are going to do is we will store the user entered string in an array and then we will check whether the extreme left and extreme right alphabets are equal or not and then from right we will move one step backward and from left we will move one step forward and then we will check again and so on, let us take an example
Our string NITIN is stored in an array str[ ] and have indexed as shown below
N — str[0]
I — str[1]
T — str[2]
I — str[3]
N — str[4]
so according to our logic we will check it as str[0]==str[4] then str[1]==str[3] and so on.
Now see the total length of the string is 5 and when we will check for palindrome as explained above the time it will be checked is also five so we can declare a counter variable in our code which will decide whether a string is a palindrome or not.
Now we get the logic now it’s time to CODE it.

CODE for palindrome:

#include<stdio.h>int main(){   int i,pal=0,len=0;   char str[100];   printf("Enter a string to check whether it is palindrome or not: ");   gets(str);   for(i=0;i<100;i++){       if(str[i]=='\0'){           break;       }       len++;   }   for(i=0;i<len;i++){       if(str[i]==str[len-1-i]){           pal++;       }   }   if(len==pal){       printf("Entered string is palindrome");   }   else{       printf("Entered string is not palindrome");   } return 0; } 

Output of the above code is shown below

Palindrom in C without using string.h

Thanks for reading

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

Понравилась статья? Поделиться с друзьями:
Добавить комментарий