If you have any query feel free to chat us!
Happy Coding! Happy Learning!
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. In C programming language, one way to check if a given string is a palindrome is to reverse the string and compare it with the original string. If they are the same, then the string is a palindrome.
Here is an example of how to check for palindrome in C:
Copy code
#include <string.h>
#include <stdio.h>
int isPalindrome(char* str) {
int n = strlen(str);
for (int i = 0; i < n / 2; i++) {
if (str[i] != str[n - i - 1]) {
return 0;
}
}
return 1;
}
int main() {
char str[] = "madam";
if (isPalindrome(str)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
In this example, the isPalindrome()
function takes a string as an argument and uses a for loop to iterate through the string, comparing the first character with the last character, the second character with the second to last character, and so on. If all the characters match, the function returns 1, indicating that the string is a palindrome. Otherwise, it returns 0.
Another approach is to reverse the string using one of the methods discussed earlier in the previous question, and then comparing the reversed string with the original string.
It's important to note that these methods consider only alphabets, if you want to consider spaces and special characters too, you need to handle them separately.
Comments: 0