forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeChecker.c
More file actions
52 lines (44 loc) · 1.61 KB
/
PalindromeChecker.c
File metadata and controls
52 lines (44 loc) · 1.61 KB
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
#include <stdio.h>
#include <string.h>
#include <stdbool.h> // Include for using the bool type (true/false)
#include <ctype.h> // Include for tolower() function
/**
* @brief Checks if a given string is a palindrome.
* * A palindrome is a word that reads the same forwards and backwards.
* This function uses a two-pointer approach to check for palindromes.
* It is case-insensitive, meaning "Racecar" is treated as a palindrome.
* * @param str The input string to check.
* @return true if the string is a palindrome, false otherwise.
*/
bool isPalindrome(char str[]) {
int left = 0;
int right = strlen(str) - 1;
// Loop until the two pointers meet in the middle
while (left < right) {
// Compare characters from both ends, ignoring case
if (tolower(str[left]) != tolower(str[right])) {
return false; // If characters don't match, it's not a palindrome
}
// Move pointers inward
left++;
right--;
}
// If the loop completes, all characters matched
return true;
}
int main() {
// Define a buffer to store the user's input string
char inputString[100];
// Prompt the user to enter a string
printf("Enter a word to check if it's a palindrome: ");
// Read the string from the user.
// scanf is used here for simplicity in a beginner example.
scanf("%s", inputString);
// Call the isPalindrome function and check the result
if (isPalindrome(inputString)) {
printf("'%s' is a palindrome.\n", inputString);
} else {
printf("'%s' is not a palindrome.\n", inputString);
}
return 0;
}