Program to check Palindrome String in C Language

Spread the love
Program-to-check-Palindrome-String-in-C-Language

Program Description:

Write a Program to Check Palindrome String in C Programming language. The Program will accept a string from the user and check whether the string is a palindrome string.

📢 This Program is part of the C String Practice Programs series

What is a Palindrome string?

If the Reverse of a String is equal to the Original String, Then the string is a Palindrome.

Example Input and Output of the Program:

Input:

Enter a string : racecar

Output:

String 'racecar' is a Palindrome

Prerequisites:

We need to know the basics of the C-Strings, C-Arrays, and Functions in C in order to understand the following program better.

We are going to look at the a few methods to check the palindrome in this article.

  1. Two-Pointer Approach(Iterative Method): Check a Palindrome string using the two-pointer approach(Without using extra space)
  2. User-Defined Functions: Check Palindrome String using a user-defined function.
  3. Reversing the string: Compare the Original String and Reverse String to detect if the string is a palindrome.

Palindrome String in C Program Explanation:

We are going to use the Two Pointer approach to check if the given string is a Palindrome string. We create two variables start and end. The start variable points to the first character in the string and the end variable points to the last character in the string.

Then go through the array and compare the characters at the start and end indices and if the characters at these indices are matched(or Equal), Then go to the next elements, Increment the start Index and Decrement the end Index. If didn’t find any mismatch, Then the string is a palindrome string.

Here is a step-by-step explanation of the program.

  1. Declare a string named inputStr with the SIZE ( The SIZE is a constant with 100 value).
  2. Take the user input and update the inputStr string.
  3. Create two variables, The start and end variables. The start variable points to the first character in the string and the end variable points to the last character in the string. i.e start = 0, end = strlen(inputStr)-1
  4. Iterate over the string and compare the characters pointed by the start and end indices.
    • At each iteration, Check if the inputStr[start] is not equal to inputStr[end], If it is true, Then the inputStr is Not a Palindrome string. Print the results and break the loop – i.e if(inputStr[start] != inputStr[end])
    • If the above condition is false, Then continue to the next iteration.
    • Increment the start index – start++
    • Decrement the end index – end--
  5. Once step 4 is completed successfully, Then we didn’t find any character anomalies, so the string is a Palindrome string.
  6. Print the results on the console.

Check a String is a Palindrome using the Iterative Method:

Let’s convert the above algorithm to C program.

Program Output:

Compile and Run the Program.

Test 1: When the string is a Palindrome

palindrome-string-in-c-program-output

As we can see from the above output, The strings tenet and racecar are palindromes.

Test 2: When the string is not a palindrome.

The CProgramming string is not a Palindrome.

Palindrome String in C using user-defined functions:

Let’s move the Palindrome check logic to a function named isPalindrome() so that we can reuse and take all advantage of the functions.

Here is the modified program to check palindrome string in C using a user-defined function.

We defined a function named isPalindrome() function. with the prototype int isPalindrome(char * inputStr). The isPalindrome() function accepts formal arguments and checks if it is a palindrome. Function returns One(1) if the inputStr is a palindrome, Otherwise function returns Zero(0).

Program Output:

Let’s Compile and Run the program.

palindrome-string-using-user-defined-function

Method 2: Check Palindrome String in C by Reversing String:

By comparing the original and reversed strings, we can determine if the given string is a palindrome string.

Here is a brief explanation of the program.

  1. Copy the input string to a New string let’s say it as Reverse string( revStr)
  2. Call the stringReverse() function with revStr to get the reverse of the input string.
  3. Compare the input string( inputStr) and reversed string( revStr) using the strcmp() function.
    1. If both strings are the same, Then the input string( inputStr) is a palindrome string.
    2. If both strings are not equal, Then the input string ( inputStr) is not a palindrome.

📢 It is recommended to use the first method, As we are using the extra space in the second method.

Program Output:

Compile and run the program using GCC (Any compiler)

check-palindrome-string-by-reversing-string

As we can see from the above output, The program is providing the desired results.

Related String Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Check Palindrome String […]

Leave a Reply