Q:

Palindrome in C

0

Palindrome in C

C program to check if a string A palindrome string is one that reads the same backward as well as forward. It can be of odd or even length.

A palindrome number is a number that is equal to its reverse.

Algorithm to check Palindrome string

  1. Copy input string into a new string (strcpy function).
  2. Reverse it (we recommend not to use strrev function as it's not compatible with C99).
  3. Compare it with the original string (strcmp function).
  4. If both of them have the same sequence of characters, i.e., they are identical, then the string is a palindrome otherwise not.

 Some palindrome strings are: "C", "CC", "madam", "ab121ba", "C++&&++C".

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#include <string.h>
int main()
{
  char a[100], b[100];

  printf("Enter a string to check if it's a palindrome\n");
  gets(a);

  strcpy(b, a);  // Copying input string
  strrev(b);  // Reversing the string

  if (strcmp(a, b) == 0)  // Comparing input string with the reverse string
    printf("The string is a palindrome.\n");
  else
    printf("The string isn't a palindrome.\n");

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now