Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

<p>Write a C language program to test if a string is a palindrome using a stack.

ID: 3628708 • Letter: #

Question

<p>Write a C language program to test if a string is a palindrome using a stack. You can push characters in the stack one by one. When you reach the end of the string, you can pop the characters and form a new string. If the two strings are exactly the same, the string is a palindrome. Note that palindromes ignore spacing, punctuation, and capitalization. Test your program with the following test cases.</p>
<p>Go dog</p>
<p>Madam, I'm Adam</p>
<p>Madam, I'm not a palindrome</p>

Explanation / Answer

please rate - thanks

#include<stdio.h>
#include<conio.h>
char pop(char[], int* );
void push(char[],int*,char) ;
int isPalindrome(char[] );
void filter(char[],char[]);
int main()
{
char input[20];
int good;
printf("Enter the string you want checked: ");
fgets(input,20,stdin);
good=isPalindrome(input);
if(good==1)
    printf("is a palindrome ");
else
     printf("is not a palindrome ");
getch();
return 0;
}
int isPalindrome(char input [])
{int i=0,j;
char input2[20];
char stack[10];
int top=-1;
filter(input,input2);

j=strlen(input2)/2;
for(i=0;i<j;i++)
     {top++;
    push(stack,&top,input2[i]);
   
    }
if(strlen(input2)%2==1)
    i++;
for(;i<strlen(input2);i++)
     {if(pop(stack,&top)!=input2[i])
            return 0;
        top--;
        }
return 1;
}
void filter(char input[],char str[])
{
int i,j=0;
for(i=0;i<strlen(input);i++)
   if(isalpha(input[i]))
        str[j++]=tolower(input[i]);
str[j]='';

}




char pop(char a[], int* n)
{ if (*n <= 0)
        return 'x';
   *n=*n-1;
   return a[*n];
}
void push(char a[], int* n, char val)
{ int maxsize=20,k;
if (*n >= maxsize)
        {printf("Stack overflow - program aborted ");
          getch();
          system("exit");
          }
   a[*n] = val;
  
*n=*n+1;

   return;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote