我已经为这个问题编写了 C++ 代码。问题基本上是用 替换开头"
和``
结尾。"
''
这很容易,但我得到了错误的答案。有人可以帮我找出我的代码的问题吗?
样本输入:
"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!"
样本输出:
``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!''
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main(){
string inputString;
while (getline(cin, inputString)){
int nDoubleQuotes = 0;
for (int i = 0 ; i < (int)inputString.length(); ++i){
if (inputString[i] == '"'){
++nDoubleQuotes;
nDoubleQuotes = nDoubleQuotes%2;
if (nDoubleQuotes == 1)
cout << "``";
else
cout << "''";
}
else
cout << inputString[i];
}
cout << '\n';
inputString.clear();
}
return 0;
}