Check if a string is palindrome in C using pointers

Suppose we have a string s. We have to check whether the given string is a palindrome or not. We have to solve this problem using pointers in C.

So, if the input is like s = "racecar", then the output will be True.

To solve this, we will follow these steps −

  • length := size of string
  • forward := pointing to the first character of string
  • reverse := pointing to the last character of string
  • while position of reverse >= position of forward, do
    • if character pointed by reverse is same as character pointed by forward, then
      • increase forward and decrease reverse by 1
    • otherwise
      • come out from loop
  • if position of forward >= position of reverse, then
    • return True
  • return False

Let us see the following implementation to get better understanding −


 Live Demo

#include <stdio.h>
#include <string.h>
int solve(char *string){
   int length;
   char *forward, *reverse;
   length = strlen(string);
   forward = string;
   reverse = forward + length - 1;
   for (forward = string; reverse >= forward;) {
      if (*reverse == *forward) {
      } else
   } if (forward > reverse)
      return 1;
      return 0;
int main(){
   char string[] = "racecar";
   printf("%d", solve(string));