In this article, we will write a C program to check if a string is a palindrome. A string is said to be palindrome if it remains the same on reading from both ends. It means that when you reverse a given string, it should be the same as the original string. For instance, the string â€˜levelâ€™ is a palindrome because it remains the same when you read it from the beginning to the end and vice versa. However, the string â€˜Simplilearnâ€™ is not a palindrome as the reverse of the string is â€˜nraelilpmis,â€™ which is not the same.

#### Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN Stack

## Algorithmic Logic of C Program for String Palindrome

To write a C program for string palindrome, we need to follow the logic below:

1. Create a function to check if the string is a palindrome: isPalindrome(str)
2. Initialize indexes for low and high levels to be 0 and n-1, respectively
3. Until the low index (l) is lower than the high index (h), do the following:
1. If str(l) is different from str(h), return false
2. If str(l) and str(h) are same, increment l, i.e., l++ and decrement h, i.e., h--
4. If we reach this step, it means there is no mismatch, and the string is a palindrome; otherwise, if step 3.A is true, it is not a palindrome.

Letâ€™s use this logic to write a C program to check if the given string is a palindrome.

#include <stdio.h>

#include <string.h>

// Implementing the logic in a function

void isPalindrome(char str[]){

// Initializing indexes

int l = 0;

int h = strlen(str) - 1;

// Step 4 to keep checking the characters until they are same

while (h > l){

if (str[l++] != str[h--]){

printf("%s is not a palindrome string\n", str);

return;

}

}

printf("%s is a palindrome string\n", str);

}

// Driver program

int main(){

isPalindrome("level");

isPalindrome("Simplilearn");

return 0;

}

#### Output:

As you can see in the above output, the strings â€˜levelâ€™ and â€˜radarâ€™ that remain the same after reversing are displayed as a palindrome. However, for this example, we gave the strings ourselves. In the coming examples, we will ask the user to enter the string and write a C program to check a string palindrome using various methods.

#### Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN Stack

## C Program for String Palindrome Using the Standard Method

We will write a C program for string palindrome using a standard method without any function or libraries. In this example, we will:

1. Read the string entered by the user using gets(s)
2. Calculate the length and store it in a variable
3. Initialize high and low indexes
4. Compare the characters at low and high index
5. Use for loop to keep comparing until a mismatch is found

Hereâ€™s the implementation of checking a string palindrome using a standard method.

#include <stdio.h>

#include <string.h>

int main(){

Â Â Â Â char str[1000];Â Â

Â Â Â Â int l,n,comp=0;Â

Â Â Â Â printf("Enter the string to check : ");

Â Â Â Â gets(str);

Â Â Â Â n=strlen(str);Â

Â Â Â Â for(l=0;l<n/2;l++){

Â Â Â Â  if(str[l]==str[n-l-1])

Â Â Â Â  comp++;

Â  }

Â  if(comp==l)

Â  Â  Â  printf("The entered string is a palindrome");

Â Â Â Â else

Â Â Â Â Â Â Â Â printf("The entered string is not a palindrome");

Â Â Â Â return 0;

}

#### Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN Stack

## C Program for String Palindrome Using a Function

The logic for writing a C program for string palindrome using a function is almost the same as that while using a standard method. The significant difference is that this time we will create a function for checking palindrome. Also, after reading the string using gets(s), we will pass it as an argument to the function.Â

Hereâ€™s the C program to check if a string is a palindrome using a function.

#include <stdio.h>

#include <string.h>

// Creating the function

int isPalindrome(char *str){

Â Â Â Â int l,comp=0,n;

Â Â Â Â n=strlen(str);Â Â

for(l=0;l<n/2;l++){

Â Â Â Â  if(str[l]==str[n-l-1])

Â Â Â Â  comp++;

Â  }Â

Â  if(comp==l)

Â Â Â Â Â Â Â Â return 1;

Â Â Â Â else

Â Â Â Â Â Â Â Â return 0;

Â }

int main(){Â Â Â Â

Â Â Â Â char str[1000];

Â Â Â Â printf("Enter the string to check: ");

Â Â Â Â gets(str);Â

Â Â Â Â if(isPalindrome(str))

Â  Â  Â  printf("The entered string is a palindrome");

Â Â Â Â else

Â Â Â Â Â Â Â Â printf("The entered string is not a palindrome");Â Â Â Â Â Â Â Â

Â Â Â Â return 0;

}

#### Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN Stack

## C Program for String Palindrome Using Recursion

The algorithm for writing a C program for string palindrome is the same with only a single major difference: the function will keep calling itself recursively until the lower index is not less than half the length of the string, i.e., l<n/2. Since the function will keep calling itself recursively, we will not have to use the for loop this time.

### Example

#include <stdio.h>

#include <string.h>

void check(char [], int);

int main(){

Â Â Â Â char str[15];

Â Â Â Â printf("Enter a word to check: ");

Â Â Â Â scanf("%s", str);

Â Â Â Â isPalindrome(str, 0);

Â Â Â Â return 0;

}Â

void isPalindrome(char str[], int l){

Â Â Â Â int n = strlen(str) - (l + 1);Â  Â

Â Â Â Â if (str[l] == str[n]){Â Â Â Â Â Â Â Â

Â Â Â Â Â Â Â Â if (l + 1 == n || l == n){

Â Â Â Â Â Â Â Â Â Â Â Â printf("The entered word is a palindrome\n");

Â Â Â Â Â Â Â Â Â Â Â Â return;

Â Â Â Â Â Â Â Â }Â  Â  Â Â

Â Â Â Â Â Â Â Â isPalindrome(str, l + 1);

Â Â Â Â }

Â Â Â Â else

Â Â Â Â {

Â Â Â Â Â Â Â Â printf("The entered word is not a palindrome\n");

Â Â Â Â }

}

## C Program for String Palindrome Using String Library Functions

For this example, we will be using three string library functions defined in the string.h header file: strcpy, strrev, and strcmp. These three functions will allow us to copy, reverse, and compare strings, the three essentials for checking a palindrome string.Â

Hereâ€™s the C program for string palindrome using string library functions.

#include <stdio.h>

#include <string.h>

int main(){

char inArray[100], revArray[100];

printf("Enter the string to check: ");

Â Â Â  scanf("%s", inArray);Â Â

// Copying input string

Â Â Â  strcpy(revArray, inArray);Â Â Â

// Reversing the string

Â Â Â  strrev(revArray);

// Comparing the reversed string with input string

if(strcmp(inputArray, reversedArray) == 0 )

printf("%s is a palindrome.\n", inputArray);

else

printf("%s is not a palindrome.\n", inputArray);Â Â Â Â Â Â Â

getch();

return 0;

}

#### Output:

Note: The above program uses the strrev() function, which is available only in ANSI C (Turbo C/C++ compilers). Hence, if you are using a standard GCC compiler, the above code might throw an error.

## Conclusion

In this article, you have learned how to write a C program for string palindrome in different ways. You can also write it using pointers in C. You can also try writing a program in C++ to check a string palindrome. C++ is an extended version of C programming. If you are new to C++, you can refer to Simplilearnâ€™s C++ Tutorial for Beginners to get a clear understanding of all the fundamentals. Besides that, you can also sign up for our SkillUp platform. The platform offers numerous free courses in different programming languages to improve your skills in any programming language, including C and C++.

You can also go for our Post Graduate Program in Full Stack Web Development, a certification course that provides training in almost 30 programming languages and tools. To put it simply, it can help you get the mastery in multiple development skills required to become a full-stack developer and land an opportunity to work for the biggest companies in the software development world.