diff --git a/StringPalindrome b/StringPalindrome new file mode 100644 index 0000000..bc5f442 --- /dev/null +++ b/StringPalindrome @@ -0,0 +1,40 @@ +#include +#include +#include +#include +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; +}