此題為出自zerojidge 基礎題a022

點我觀看題目

以下是我寫出來的答案




#include <iostream>
using namespace std;

// 定義 isPalindrome 函數
void isPalindrome(string word) ;

int main() 
{
    string word;
    cin >> word;  // 正確讀取字串
    isPalindrome(word);  // 調用 isPalindrome 函數
    return 0;
}

void isPalindrome(string word) 
{
    int length = word.length();
    bool isPalin = true;

    for (int i = 0; i < length / 2; i++) 
	{
        if (word[i] != word[length - 1 - i]) 
		{
            isPalin = false; // 若找到不匹配的字符,則非迴文
            break;
        }
    }

    if (isPalin) 
	{
        cout << "yes";
    } 
	else 
	{
        cout << "no";
    }
}






點我看程式碼

 

 

 

 

arrow
arrow
    全站熱搜

    戰昇 發表在 痞客邦 留言(0) 人氣()