Upload files to "DS/C/Lab/Week3"
This commit is contained in:
parent
c23e09cb90
commit
a033c5175f
4 changed files with 194 additions and 0 deletions
46
DS/C/Lab/Week3/StackPalindrome.c
Normal file
46
DS/C/Lab/Week3/StackPalindrome.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include <stdio.h>
|
||||
#define MAX_SIZE 100
|
||||
|
||||
int top = -1;
|
||||
char stack[MAX_SIZE];
|
||||
|
||||
void push(char c) {
|
||||
if (top < MAX_SIZE - 1) {
|
||||
stack[++top] = c;
|
||||
}
|
||||
}
|
||||
|
||||
char pop() {
|
||||
if (top >= 0) {
|
||||
return stack[top--];
|
||||
}
|
||||
return '\0';
|
||||
}
|
||||
|
||||
int is_palindrome(char *str) {
|
||||
int i;
|
||||
for (i = 0; str[i] != '\0'; i++) {
|
||||
push(str[i]);
|
||||
}
|
||||
|
||||
for (i = 0; str[i] != '\0'; i++) {
|
||||
if (str[i] != pop()) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char str[MAX_SIZE];
|
||||
printf("Enter a string: ");
|
||||
scanf("%s", str);
|
||||
|
||||
if (is_palindrome(str)) {
|
||||
printf("%s is a palindrome.\n", str);
|
||||
} else {
|
||||
printf("%s is not a palindrome.\n", str);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue