1. 程式人生 > >6-2 判斷迴文字串(20 分)

6-2 判斷迴文字串(20 分)

6-2 判斷迴文字串(20 分)

本題要求編寫函式,判斷給定的一串字元是否為“迴文”。所謂“迴文”是指順讀和倒讀都一樣的字串。如“XYZYX”和“xyzzyx”都是迴文。

函式介面定義:

bool palindrome( char *s );

函式palindrome判斷輸入字串char *s是否為迴文。若是則返回true,否則返回false

裁判測試程式樣例:

#include <stdio.h>
#include <string.h>

#define MAXN 20
typedef enum {false, true} bool;

bool palindrome( char *s );

int main()
{
    char s[MAXN];

    scanf("%s", s);
    if ( palindrome(s)==true )
        printf("Yes\n");
    else
        printf("No\n");
    printf("%s\n", s);

    return 0;
}

/* 你的程式碼將被嵌在這裡 */

輸入樣例1:

thisistrueurtsisiht

輸出樣例1:

Yes
thisistrueurtsisiht

輸入樣例2:

thisisnottrue

輸出樣例2:

No
thisisnottrue
bool palindrome(char *s)
{
	int n=strlen(s);
	int i, j,count=0;
	for (i = 0, j = n - 1; i < n, j >= 0; i++, j--)
	{
		if (*(s + i) == *(s + j))
		{
			count++;
		}
	}
	if (count == n)
		return true;

}


作者: C課程組 單位: 浙江大學 時間限制: 400ms 記憶體限制: 64MB 程式碼長度限制: 16K