C课程设计 编写程序,判断一字符串是否为回文,回文即顺读和逆读都一样的字符串,如madam,123321。

如题所述

、int Palindrome_Test()//判别输入的字符串是否回文序列,是则返回1,否则返回0
{
InitStack(S);InitQueue(Q);
while((c=getchar()!='@')
{
Push(S,c);EnQueue(Q,c); //同时使用栈和队列两种结构
}
while(!StackEmpty(S))
{
Pop(S,a);DeQueue(Q,b));
if(a!=b) return ERROR;
}
return OK;
}//Palindrome_Test
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-07-03
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define len sizeof(stack)
#define null 0
typedef struct node
{
char data;
struct node *next;
}stack;

int n;//记录栈中数据的个数;

//初始化栈
stack *initstack()
{
stack *top;
top=null;
return(top);
}
//进栈操作
stack *push(stack *top,char strl[100])
{
stack *p;
int i=0;
p=(stack *)malloc(len);
while(strl[i]!='#')
{
if(i<strlen(strl)/2)
{
p->data=strl[i];
p->next=top;
top=p;
i++;
p=(stack *)malloc(len);
n++;
}
else
break;
}
printf("栈中的数据有%d个\n",n);
return(top);
}

//出栈运算
stack *pop(stack *top,char strl[100])
{
int i=strlen(strl)/2+1;
if(strlen(strl)%2==0)
{
while(top!=null)
{
if(top->data!=strl[i])
printf("这个字符串不是回文!\n");
goto zhou;
top=top->next;
n--;
}
zhou:
if(n!=0)
printf("这个字符串是回文!\n");
}
else
{
while(top!=null)
{
if(top->data!=strl[i])
printf("这个字符串不是回文!\n");
goto cheng;
top=top->next;
n--;
}
cheng:
if(n!=0)
printf("这个字符串是回文!\n");
}
return(top);
}
void main()
{
stack *top;
char strl[100];
top=initstack();
printf("请输入要判断的字符串:!\n");
scanf("%s",&strl);
top=push(top,strl);
top=pop(top,strl);
printf("\n");
}
第2个回答  2009-07-03
#include<stdio.h>
#include<string.h>
#define N 20

int main()
{
char s[N];
int i,len;

scanf("%s",&s);
len=strlen(s);

for(i=0;i<len/2;i++)
{
if(s[i]!=s[len-i-1])
{
printf("No.\n");
break;
}
}

if(i>=len/2)
{
printf("Yes.\n");
}

return 0;
}
相似回答