0

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;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
AnkitSablok
  • 3,021
  • 7
  • 35
  • 52
  • 1
    You should really include enough explanation of what you're trying to do that your question will remain meaningful if the link goes dead. – Jerry Coffin Jul 24 '13 at 04:57
  • Hello, i think the problem is getline. Please check my updated answer. – keelar Jul 24 '13 at 05:38
  • Btw, simpler than having an integer, incrementing it and limiting it to modulo 2 would be to have a `bool inQuotes = false;` and instead of increment and clipping, just do `inQuotes = !inQuotes;` to flip the flag. – Sebastian Redl Jul 24 '13 at 17:19

2 Answers2

2

Unfortunately your code do not even pass sample testcase! Anyways, just put this line, int nDoubleQuotes = 0; out of the while( getline( cin , inputString ) ) loop, the reason you need to do it is, In the input file a quotation mark (") can start in one line and can end in any other following line, as the sample test case showed in the problem statement:

The programming contestant replied: "I must disagree. #quote start on this line
To `C' or not to `C', that is The Question!" #quote ends on this

If you initialize quote counter variable on every line then you are assuming, quote marker start and end on the same line, which is wrong.

sowrov
  • 1,018
  • 10
  • 16
  • Good job :D, I didn't see that! – keelar Jul 24 '13 at 05:46
  • @sowrov : Although I got an AC with that but still I would like to know why can't I include the double Quotes variable inside the while loop, I got correct answers for sample inputs and for many other test cases I tried, I thought it had the same effect as with the one outside the while loop, at an odd number of quotes nDoubleQuotes would be 1 and as every sentence ended at the even number of quotes at the end of the sentence nDoubleQuotes has to be 0, and the same thing I tried to achieve individually for every string, what went wrong? can you explain? – AnkitSablok Jul 24 '13 at 08:59
  • The problem is that you restart the outer loop for every line in the input. If the `nDoubleQuotes` variable is inside the loop, it will go out of scope and reappear (and be reinitialized to 0) for the next iteration. In other words, you're resetting the "am I in quotes" state for every line of text. – Sebastian Redl Jul 24 '13 at 17:18
1

You can solve this just reading a character at a time. You need to keep track of whether you are inside a quote or not in order to print the correct replacement for ".

#include <iostream>

int main()
{
    bool inquote = false;
    char ch;
    while (std::cin.get(ch)) {
        if (ch == '"') {
            std::cout << (inquote ? "''" : "``");
            inquote = !inquote;
        } else {
            std::cout << ch;
        }
    }
}
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70