Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions StringPalindrome
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
bool isPalindrome(char str[]) {
int left = 0;
int right = strlen(str) - 1;
while (left < right) {
// Ignore non-alphanumeric characters from both ends
while (!isalnum(str[left]) && left < right) {
left++;
}
while (!isalnum(str[right]) && left < right) {
right--;
}
// Compare the characters (case-insensitive)
if (tolower(str[left]) != tolower(str[right])) {
return false;
}
left++;
right--;
}
return true;
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove the newline character if present
int len = strlen(str);
if (str[len - 1] == '\n') {
str[len - 1] = '\0';
}
if (isPalindrome(str)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}