-->
当前位置:首页 > 题库 > 正文内容

编程题:判断回文

Luz4年前 (2021-10-17)题库995
回文是指正读反读均相同的字符序列,如“abba”和“abdba”均是回文,但“good”不是回文。试写一个程序判定给定的字符向量是否为回文,用栈实现。(提示:将一半字符入栈)

### 输入格式:

输入任意字符串。

### 输出格式:

若字符串是回文,输出:xxxx是回文。
若字符串不是回文,输出:xxxx不是回文。

### 输入样例:

in
abba


### 输出样例:

out
abba是回文。


### 输入样例:
in
abdba



### 输出样例:
out
abdba是回文。


### 输入样例:
in
good


### 输出样例:
out
good不是回文。






答案:若无答案欢迎评论

评论列表

LZ1123
LZ1123
3年前 (2022-04-17)

#include
#include
#define StackSize 10
#define MAXSIZE 10
typedef struct
{
char data[StackSize];
int top;
}SqStack;
//初始化栈
void InitStack(SqStack *S)
{
S-˃top=-1;
}
//栈的判空
int StackEmpty(SqStack S)
{
if (S.top==-1)
return 1;
else
return 0;
}
//进栈
int Push(SqStack *S,char e)
{
if(S-˃top == StackSize -1)
{
return 0;
}
S-˃top++;
S-˃data[S-˃top]=e;
return 1;
}
//出栈
char Pop(SqStack *S)
{
if(S-˃top==-1)
return 0;
char e=S-˃data[S-˃top];
S-˃top--;
return e;
}
int JudgeHuiWen(char *str)
{
SqStack s;
InitStack(&s);
int i;
char temp;
int len=strlen(str);
for(i=0;i˂len/2;i++)
Push(&s,str[i]);
if(len%2==1)i++;//当字符个数为奇数时跳过中间字符
while(!StackEmpty(s))
{
temp=Pop(&s);
if(temp!=str[i]) return 0;
else i++;
}
return 1;
}
int main()
{
char str[MAXSIZE];
gets(str);
int judge=JudgeHuiWen(str);
if(judge==1)
printf("%s是回文。",&str);
else
printf("%s不是回文。",&str);
}

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。