C Program to Check Whether a String is Palindrome or Not ? (String Palindrome Program in C Language)

A palindrome string is a string that reads the same forward and backward. For example, MADAM, LEVEL and RADAR are palindrome strings. A string that does not read the same forward and backward is called a non-palindrome string.

Palindrome String वह String होती है जो आगे और पीछे दोनों तरफ से पढ़ने पर समान होती है। उदाहरण के लिए MADAM, LEVEL और RADAR Palindrome Strings हैं।

C Program

#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
int i;
printf("Enter a string: ");
fgets(a, sizeof(a), stdin);
a[strcspn(a, "\n")] = '\0';
strcpy(b, a);
// Reverse the copied string
for (i = 0; b[i] != '\0'; i++)
{
// Find string length
}
for (int j = 0, k = i - 1; j < k; j++, k--)
{
char temp = b[j];
b[j] = b[k];
b[k] = temp;
}
if (strcmp(a, b) == 0)
printf("%s is a palindrome string.", a);
else
printf("%s is not a palindrome string.", a);
return 0;
}

Output 1

Enter a string: MADAM
MADAM is a palindrome string.

Output 2

Enter a string: COMPUTER
COMPUTER is not a palindrome string.

Explanation

  1. Two character arrays a and b are declared to store the original and reversed strings.

  2. fgets() accepts a string from the user.

  3. strcpy() copies the original string into b.

  4. The loop reverses the copied string by swapping its characters.

  5. strcmp() compares the original string with the reversed string.

  6. If both strings are equal, the string is a palindrome; otherwise, it is not.

हिंदी में व्याख्या:

  1. Original और Reversed String को Store करने के लिए दो Character Arrays a और b Declare किए गए हैं।

  2. fgets() यूजर से String Input लेता है।

  3. strcpy() Original String को b में Copy करता है।

  4. Loop की सहायता से दूसरी String के Characters को Reverse किया जाता है।

  5. strcmp() Original और Reversed Strings की तुलना करता है।

  6. यदि दोनों Strings समान हैं, तो String Palindrome है, अन्यथा नहीं।

Important Points

  • A palindrome reads the same forward and backward.

  • strcpy() copies a string.

  • strcmp() compares two strings.

  • This program performs a case-sensitive comparison. For example, Madam is not considered a palindrome because uppercase and lowercase letters are different.

महत्वपूर्ण बिंदु:

  • Palindrome String आगे और पीछे से समान होती है।

  • strcpy() String को Copy करता है।

  • strcmp() दो Strings की तुलना करता है।

  • यह Program Case-Sensitive है। उदाहरण के लिए Madam को Palindrome नहीं माना जाएगा क्योंकि बड़े और छोटे अक्षर अलग होते हैं।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025