C Program To Check A String Is Palindrome Or Not

In this tutorial,i am going to show you how to check a string is a palindrome or not with the help of for loop and functions in c programs.

All C Programs and Algorithm To Check A String Is Palindrome Or Not

  • Algorithm to Check a Strng is palindrom or Not
  • C Program To Check A String Is Palindrome Or Not using For Loop
  • C Program To Check A String Is Palindrome Or Not using Function

Algorithm to Check a Strng is palindrom or Not

Just follow the below given algorithm to write to check a whether a given string is palindrome or not; as follows:

  • Start Program
  • Take a string as input and store it in the array.
  • Reverse the string and store it in another array using for loop or function.
  • Compare both the arrays.
  • Print result
  • End Program

C Program To Check A String Is Palindrome Or Not using For Loop

 #include <stdio.h>
#include <string.h>
 
int main()
{
    char s[1000];  
    int i,n,c=0;
 
    printf("Enter  the string : ");
    gets(s);
    n=strlen(s);
 
    for(i=0;i<n/2;i++)  
    {
    	if(s[i]==s[n-i-1])
    	c++;
 
 	}
 	if(c==i)
 	    printf("string is palindrome");
    else
        printf("string is not palindrome");
 
 	 
     
    return 0;
}

The result of the above c program; as follows:

Enter  the string : hello
string is not palindrome

C Program To Check A String Is Palindrome Or Not using Function

 #include <stdio.h>
#include <string.h>
 
int checkpalindrome(char *s)
{
    int i,c=0,n;
    n=strlen(s);
	for(i=0;i<n/2;i++)  
    {
    	if(s[i]==s[n-i-1])
    	c++;
 
 	}
 	if(c==i)
        return 1;
    else
        return 0;
 
 	
	  
 }
int main()
{
 
    char s[1000];  
   
    printf("Enter  the string: ");
    gets(s);
    
 
    if(checkpalindrome(s))
 	    printf("string is palindrome");
    else
        printf("string is not palindrome");
 
     
     
}

The result of the above c program; as follows:

Enter  the string: madam
string is palindrome

More C Programming Tutorials

Leave a Comment