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 stringfor (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);elseprintf("%s is not a palindrome string.", a);return 0;}
Output 1
Enter a string: MADAMMADAM is a palindrome string.
Output 2
Enter a string: COMPUTERCOMPUTER is not a palindrome string.
Explanation
Two character arrays
aandbare declared to store the original and reversed strings.fgets()accepts a string from the user.strcpy()copies the original string intob.The loop reverses the copied string by swapping its characters.
strcmp()compares the original string with the reversed string.If both strings are equal, the string is a palindrome; otherwise, it is not.
हिंदी में व्याख्या:
Original और Reversed String को Store करने के लिए दो Character Arrays
aऔरbDeclare किए गए हैं।fgets()यूजर से String Input लेता है।strcpy()Original String कोbमें Copy करता है।Loop की सहायता से दूसरी String के Characters को Reverse किया जाता है।
strcmp()Original और Reversed Strings की तुलना करता है।यदि दोनों 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,
Madamis not considered a palindrome because uppercase and lowercase letters are different.
महत्वपूर्ण बिंदु:
Palindrome String आगे और पीछे से समान होती है।
strcpy()String को Copy करता है।strcmp()दो Strings की तुलना करता है।यह Program Case-Sensitive है। उदाहरण के लिए
Madamको Palindrome नहीं माना जाएगा क्योंकि बड़े और छोटे अक्षर अलग होते हैं।
Comments
Post a Comment