Search code examples
c++string-comparison

What is the problem with the following c++ code?


The problem is 12289 - One-Two-Three from Online Judge at https://onlinejudge.org/external/122/12289.pdf I have to compare a given string s with the following: "one","two","three", and return a number that indicates which of those has the most correct characters in a correct position with the string. The following is my attempt at getting an accepted answer.

#include <cstdio>
using namespace std;
int main(){
    char c;
    int t,len,c1,c2;
    scanf("%d\n",&t);
    while(t--){
        len = 0;
        c1 = 0;
        c2 = 0;
        while(true){
            scanf("%c",&c);
            if(c=='\n') break;
            if("one"[len] == c) c1++;
            if("two"[len] == c) c2++;
            len++;
        }
        if(len>3) printf("%d\n",3);
        else if (c1>c2) printf("%d\n",1);
        else printf("%d\n",2);
    }
    printf("\n");
}

I am getting a "Wrong answer" in this question, that usually does not involve formatting problems. I am new to C++ so it would help me a lot to know in what can I improve.


Solution

  • Thank you for your support. I discovered that the real problem was the formatting of the input. There were blank lines between two consecutive inputs, so the part of if(c=='\n') break; was causing some trouble. Anyways, I will try to remake the solution following C++ guidelines. I just did not know how to process a string, so I thought of doing it character by character. I'm closing the thread.