给定一个字符串,判定是否是回文数,如abcba是回文数,abc不是
实例输入
abcba
实例输出
abcba是一个回文数
代码:
/*
本实验完成验证一个字符串是否为回文字符串
*/
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
#define max 10
int top = 100;
void init_stack(char s[])
{
top = 1;
}
int notempty(char s[])
{
if(top <= 1){
return 0;
}else return 1;
}
void push(char s[],char c)
{
s[top] = c;
top ++;
}
char pop(char s[])
{
if(notempty(s)){
top = top - 1;
return s[top];
}
}
int main()
{
char s[max];
gets(s);
int lenth = strlen(s);
//printf("%d",lenth);
char a[top];
init_stack(a);
int i;
for(i = 0; i<lenth/2;i++){
push(a,s[i]);
}
int l = (lenth+1)/2,cnt = 1;
char temp;
while(notempty(a)){
temp = pop(a);
//printf("%c",temp);
if(temp == s[l]){
l ++;
continue;
}else{
cnt = 0;
break;
}
break;
}
if(cnt == 0){
printf("%s不是一个回文字符串",s);
}else{
printf("%s是一个回文字符串",s);
}
return 0;
}