C语言 字符回文

#include<stdio.h>
#include<string.h>
#define N 100
void main()
{
int i,n=0,flage=1;
char st[N];
gets(st);

n=strlen(st);

if(n%2==0)
{
for(i=0;i<n/2;i++)

if(strcmp(st[i],st[n-i-1])!=0)
{
flage=0;
break;
}

}

else
{
for(i=0;i<(n-1)/2;i++)
if(strcmp(st[i],st[n-i-1])!=0)
{
flage=0;
break;
}

}

if(flage=0)
printf("no\n");
else
printf("yes\n");
}
希望能够判断任意输入的字符串是否为回文
请高人指点哪出错了

/*1.假设称正读和反读都相同的字符序列为"回文",例如,"abcddcba"、 "qwerewq"是回文,"ashgash"不是回文。
是写一个算法判断读入的一个以'@'为结束符的字符序列是否为回文。*/
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 100
typedef char DataType;
typedef struct{
DataType data[MAXSIZE];
int top;
}SeqStack,*PseqStack;//定义一个线性表栈。
typedef struct {
DataType data[MAXSIZE];
int front,rear;
}SeqQueue,*PseqQueue;//定义一个线性表队。
PseqQueue Init_SeqQueue()
{
PseqQueue Q;
Q=(PseqQueue)malloc(sizeof(SeqQueue));
if(Q)
{
Q->front=0;
Q->rear=0;
}
return Q;
}//队列初始化.
PseqStack Init_SeqStack(void)
{
PseqStack S;
S=(PseqStack)malloc(sizeof(SeqStack));
if(S)
S->top=-1;
return(S);
}//初始化栈。
int Push_SeqStack(PseqStack S,DataType x)
{
if(S->top==MAXSIZE-1)
return (0);
else
{
S->top++;
S->data[S->top]=x;
return (1);
}
}//入栈。
int Empty_SeqStack(PseqStack S)
{
if(S->top==-1)
return(1);
else return (0);
}//判断是否栈空。
int Pop_SeqStack(PseqStack S,DataType *x)
{
if(Empty_SeqStack(S))
return (0);
else
{
*x=S->data[S->top];
S->top--;
return (1);
}
}//出栈。

int In_SeqQueue(PseqQueue Q,DataType x)
{
if((Q->rear+1)%MAXSIZE==Q->front)
{
printf("队满");
return (-1);
}
else
{
Q->rear=(Q->rear+1)%MAXSIZE;
Q->data[Q->rear]=x;
return 1;
}
}//入队。
int Empty_SeqQueue(PseqQueue Q)
{
if(Q&&Q->front==Q->rear)
return 1;
else return 0;
}//判断队空。
int Out_SeqQueue(PseqQueue Q,DataType *x)
{
if(Empty_SeqQueue(Q))
{
printf("队空");
return (-1);
}
else
{
Q->front=(Q->front+1)%MAXSIZE;
*x=Q->data[Q->front];
return 1;
}
}//出队。
int huiwen(char *ch)
{
char x,y;
int count;
PseqStack S;
PseqQueue Q;
S=Init_SeqStack();
Q=Init_SeqQueue();
while(*ch!='\0')
{
Push_SeqStack(S,*ch);
In_SeqQueue(Q,*ch);
ch++;
}
count=1;
while(!Empty_SeqStack(S)&&!Empty_SeqQueue(Q))
{
Pop_SeqStack(S,&x);
Out_SeqQueue(Q,&y);
if(x!=y)
{
count=0;
break;
}
}
if(count)
{
printf("是回文。\n");
return 1;
}
else
{
printf("不是回文。\n");
return 0;
}
}//主算法。
void main()
{
char ch[MAXSIZE];
printf("输入一个符串:");
gets(ch);
huiwen(ch);
}//主函数。
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-11-30
#include<stdio.h>
#include<string.h>
#define N 100
void main()
{
int i,n=0,flage=1;
char st[N];
gets(st);

n=strlen(st);

for(i=0;i<n/2;i++) // 其实不用判断奇数和偶数的
{ // 比如 5/2 ==2 4/2 ==2 但问题不出在这
// 你那样分开也一样的
if(strcmp(st[i],st[n-i])!=0)//问题出在这里 应该是st[n-i]
{
flage=0;
break;
}
}

if(flage=0)
printf("no\n");
else
printf("yes\n");
}本回答被提问者采纳
第2个回答  2010-05-16
//少改动原代码。嘿嘿。
#include<stdio.h>
#include<string.h>
#define N 100
void main()
{
int i,n=0,flage=1;
char st[N];
gets(st);
n=strlen(st);
if(n%2==0)
{
for(i=0;i<n/2;i++)
if( st[i]!=st[n-i-1] )///这
{
flage=0;
break;
}

}

else
{
for(i=0;i<(n-1)/2;i++)
if(st[i]!=st[n-i-1] )//这
{
flage=0;
break;
}

}

if(flage==0)//这,
printf("no\n");
else
printf("yes\n");
}
第3个回答  2010-05-16
#include<stdio.h>
#include<string.h>

int fun( char str[] )
{
int i,j,n;
n=strlen(str);

for(i=0,j=n-1;i<=(n-1)/2;i++,j--)
if(str[i]!=str[j]) break;
if(i>j) return 1;
return 0;

}

void main()
{
char a[80];
gets(a);
if ( fun(a)==1 ) printf( "回文\n");
else printf("非回文\n");
}
看看这个吧
相似回答