Palindrome in C

  • برمجة سي c

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".

الأجوبة

#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;
}
هل كان المحتوى مفيد؟

القوائم الدراسية التي ينتمي لها السؤال

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...