/*The implementation of the stack requires only a one-dimensional array and a pointer to the top of the stack, the stack is inserted and deleted through top, the following code is to calculate whether the string is a palindrome*/#include<stdio.h>#include<string.h>intMain () {Chara[101],s[101]; intI,len,mid,next,top; Gets (a); //read a line stringLen=strlen (a);//to find the length of a stringmid=len/2-1;//to find the midpoint of a stringTop=0;//initialization of the stack//move the characters in front of mid to the stack for(i=0; i<=mid;i++) s[++top]=A[i]; //determine whether the length of the string is an odd or even number, and identify the starting subscript that requires a character match if(len%2==0) Next=mid+1; ElseNext=mid+2; //Start Matching for(i=next;i<=len-1; i++) { if(a[i]!=S[top]) Break; Top--; } //if the value of top is 0, then all the characters in the stack are matched by one by one. if(top==0) printf ("YES"); Elseprintf ("NO"); return 0;}
Palindrome---Stack