C Program to Check if a Given String is Palindrome

      No Comments on C Program to Check if a Given String is Palindrome

C Program to Check if a Given String is Palindrome – Given a string, we write a c function to check whether that string is palindrome or not.

A string is said to palindrome if the string and it’s reverse are equal. For example, “aa”,”abbccbba” are palindromes where as, “aac” , “abcd” are not.

Algorithm for – C Program to Check if a Given String is Palindrome

isPalindrome(string)
1. Find the length of string and store it in a variable say 'len'.
2. Initialize two variables start and end as 0 and len-1 respectively.
3. Do while start<end
     if string[start] != string[end]
         return false
     if string[start] == string[end]
         return true

Following is the C implementation to check if the string is palindrome or not :

#include
#include
void isPalindrome(char string[])
{
int start=0;
int end=strlen(string)-1;
while(end>start)
{
if(string[start++]!=string[end--])
{
printf("\nString %s is not a palindrome",string);
return;
}
}
printf("\n Given string %s is a palindrome",string);
}
int main()
{
isPalindrome("abaca");
isPalindrome("aabbccaa");
return(0);
}

C Program to Check if a Given String is Palindrome

Summary
C Program to Check if a Given String is Palindrome
Article Name
C Program to Check if a Given String is Palindrome
Description
C Program to Check if a Given String is Palindrome - Given a string, we write a c function to check whether that string is palindrome or not.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *