【九度OJ】1192:迴文字符串

地址:
http://ac.jobdu.com/problem.php?pid=1192
題目描述:
給出一個長度不超過1000的字符串,判斷它是不是迴文(順讀,逆讀均相同)的。
輸入:
輸入包括一行字符串,其長度不超過1000。
輸出:
可能有多組測試數據,對於每組數據,如果是迴文字符串則輸出”Yes!”,否則輸出”No!”。
樣例輸入:
hellolleh
helloworld
樣例輸出:
Yes!
No!
來源:
2007年華中科技大學計算機研究生機試真題

源碼:

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

int main(){
    char str[ 1005 ];
    int len = 0;
    int halfLen = 0;
    int isPalindrome = 1;

    while( scanf( "%s", &str ) != EOF ){
        len = strlen( str );
        halfLen = len / 2;

        isPalindrome = 1;
        for( int i = 0; i < halfLen; i++ ){
            if( str[ i ] != str[ len - 1 - i ] ){
                isPalindrome = 0;
                break;
            }
        }

        if( isPalindrome == 1 ){
            printf("Yes!\n");
        }
        else{
            printf("No!\n");
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章