-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome_checker.c
69 lines (55 loc) · 1.76 KB
/
palindrome_checker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LENGTH 100
// Function to check if the string is a palindrome
int isPalindrome(char str[]) {
int start = 0;
int end = strlen(str) - 1;
while (start < end) {
// Skip non-alphanumeric characters
while (start < end && !isalnum((unsigned char)str[start])) {
start++;
}
while (start < end && !isalnum((unsigned char)str[end])) {
end--;
}
// Compare characters
if (tolower((unsigned char)str[start]) != tolower((unsigned char)str[end])) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1; // Is a palindrome
}
int main() {
char str[MAX_LENGTH];
char choice;
do {
printf("Enter a string: ");
if (fgets(str, sizeof(str), stdin) == NULL) {
printf("Error reading input.\n");
return 1;
}
// Remove newline character from fgets
str[strcspn(str, "\n")] = '\0';
// Check if the input string is empty
if (strlen(str) == 0) {
printf("You entered an empty string. Please enter a valid string.\n");
continue;
}
// Check if the string is a palindrome
if (isPalindrome(str)) {
printf("The input string is a palindrome.\n");
} else {
printf("The input string is not a palindrome.\n");
}
// Ask the user if they want to continue
printf("Do you want to check another string? (y/n): ");
choice = getchar();
while (getchar() != '\n'); // Clear the newline character left in the buffer
} while (choice == 'y' || choice == 'Y');
printf("Exiting the program.\n");
return 0;
}