❶ c語言判斷字元串是否為迴文
#include <stdio.h>
/*fun()函數:傳入一個字元數組,如果是迴文序列返回1,不是就返回0*/
int fun(char a[])
{
int i,j,n=0;
while(a[n]!='\0') n++; /*計算傳入字元串(數組)長度*/
n--; /*跳出while循環時 a[n]='\0',故n--*/
for(i=0,j=n;i<j;i++,j--)
if(a[i]!=a[j]) break;
if(i>=j) return 1;
return 0;
}
int main()
{
char str[20];
puts("輸入一個字元串:\n");
gets(str);
if(fun(str)) printf("%s 是迴文序列\n",str);
else printf("%s 不是迴文序列\n",str);
return 0;
}
❷ C語言編寫一個程序,判斷輸入的一個字元串是否是迴文。
源代碼如下:
#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("輸入一個整數: ");
scanf("%d", &n);
originalInteger = n;
// 翻轉
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// 判斷
if (originalInteger == reversedInteger)
printf("%d 是迴文數。", originalInteger);
else
printf("%d 不是迴文數。", originalInteger);
return 0;
}
(2)c語言字元串是迴文嗎擴展閱讀
1、函數的返回值是通過函數中的return語句獲取的。
2、函數值的類型。
3、如果函數值的類型和return語句中表達式的值不一樣,則以函數類型為准。
4、如果調用函數中沒有return語句,並不帶回一個確定的用戶需要的值,函數不是不帶回值,而只是不帶回有用的值,帶回一個不確定的值。
❸ C語言,判斷一個字元串是不是迴文,不知道錯在哪裡,求解。
str2 是數組名,指向數組的地址,是常量,不可更改。所以這里錯了:str2 = strrev(str1);
你可以用循環把值一個個的賦給str2.
❹ C語言 判斷一個字元串是不是迴文
while是找到字元串的末尾,'\n'是所有字元串的最後一個字元,for循環是用來判斷是不會迴文的。j是從第一個字元開始,i是從最後一個字元開始,比較完一個字元後,j++,i--,一旦發現某個地方首位是不相等的,則不是迴文退出
❺ C語言 字元迴文
/*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);
}//主函數。
❻ c語言編程題 輸入一字元串,判斷該字元串是否為迴文。
#include <stdio.h>
#include<stdlib.h>
int main()
{
char a[100];
int i=0,j=0;
printf("請輸入字元串: ");
gets(a);
while(a[i]!='