In: Computer Science
Write a program that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks.
In Pseudocode please
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* stack;
int top = -1;
//Push function
void push (char ele){
stack [++top] = ele;
}
//Pop function
char pop() {
return stack [top--];
}
//Function that returns 1
//If str is a palindrome
int isPalindrome (char str[]) {
int length = strlen(str);
//Allocating the memory for the stack
stack= (char*)malloc(length * size of(char));
//Finding the mid
int i,mid = length/2;
for(i=0;i<mid;i++) {
push(str[i]);
//Checking if the length of the string
// is odd, if odd then neglect the
//middle character
if(length % 2 != 0) {
i++;
}
/)While not the end of the string
while(str[I] != '\0') {
char ele = pop();
//If the character differ then the
//given string is not a palindrome
if (ele != str[i])
return 0;
i++;
}
return 1;
//Driver code
main ()
char str[] = "madam";
if(isPalindrome (str)) {
print("Yes");
}
else {
print("No");
}return 0;
}
Output:- Yes