I have written C++ code for this problem. The problem is basically to replace an opening " with `` and a closing " with ''.
It is quite easy but I am getting wrong answer. Can someone help me find a problem with my code?
Sample input:
"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!"
Sample output:
``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!''
Code:
#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;
}
 
     
     
    