紫书P45 例题3-1
一、题意
输入一篇文章,把左右引号" "分别替换为 ``和 ' '。
二、解析
边输入边判断,然后边输出即可。
三、代码
#include #include using namespace std; int main(){ char ch; const string q[2] = {"``", "''"}; int idx = 0; while((ch = getchar()) != EOF) { if(ch == '"') cout << q[idx], idx = (idx + 1) % 2; else cout << ch; } } /* "To be or not to be," quoth the Bard, "that is the question". The programming contestant replied: "I must disagree. To `C' or not to `C', that is The Question!" */
四、归纳
- 字符输入:getchar() 不要用cin
- 字符文尾判断: ch != EOF
- ps: 输出字符:putchar()
回到最初