-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome check using Stack.c
More file actions
56 lines (49 loc) · 1.03 KB
/
Copy pathPalindrome check using Stack.c
File metadata and controls
56 lines (49 loc) · 1.03 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
53
54
55
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX 100
char stack[MAX];
int top = -1;
void push(char c) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = c;
}
char pop() {
if (top == -1) {
printf("Stack Underflow\n");
return '\0';
}
return stack[top--];
}
int isPalindrome(char str[]) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (isalnum(str[i])) {
push(tolower(str[i]));
}
}
for (int i = 0; i < len; i++) {
if (isalnum(str[i])) {
char c = pop();
if (tolower(str[i]) != c) {
return 0;
}
}
}
return 1;
}
int main() {
char str[MAX];
printf("Enter a string: ");
fgets(str, MAX, stdin);
str[strcspn(str, "\n")] = '\0';
if (isPalindrome(str))
printf("The string is a palindrome.\n");
else
printf("The string is NOT a palindrome.\n");
return 0;
}