# 題解:a001. 哈囉 ### 題目:https://zerojudge.tw/ShowProblem?problemid=a001 ### 解題思路: C 語言: 因為 ZeroJudge 無法使用 <conio.h> 函式庫,所以無法使用 getch() 函式一次讀取一個字元並輸出,所以選擇宣告一個有一定長度的字元陣列 (也就是字串,後面會提到),用來存放讀取的字串。 另外一個寫法是多加入 <stdlib.h> 以使用 EOF (end of file)。因為scanf() 函式其實是有回傳值的,所以如果我們判斷他的回傳值如果不是 EOF,代表我們可以繼續往後讀取;如果等於 EOF,代表已經讀完,就可以結束迴圈。<b>和上面的方法相比,用此方法可以確保讀入的字串沒有長度限制,不用擔心字串長度超過我們給定的陣列大小。</b> C++: 直接使用 string 型態宣告字串來處理。 ### 程式碼 (C語言,使用字元陣列): ```cpp= #include <stdio.h> int main(void) { char c[100]; scanf(" %s", c); printf("hello, %s", c); } ``` ### 程式碼 (C語言,使用 EOF): ```cpp= #include <stdio.h> #include <stdlib.h> /* 要加入此函式庫才能使用 EOF */ int main() { char c; printf("hello, "); while (scanf("%c", &c) != EOF){ printf("%c", c); } } ``` ### 程式碼 (C++): ```cpp= #include <iostream> using namespace std; int main(void) { string s; cin >> s; cout << "hello, " << s; } ```