Palindrome---Stack

Source: Internet
Author: User

/*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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.