0

I've started working on hackerrank/hackerearth like sites. There i've found one problem fetching input. Its easy in Java.

Consider a very simple problem of reading input and displaying it : Flow is like :

read A
repeat A times
      read X1,N1,X2,N2 ; where Xi is any string, Ni is any integer.
      display X1+X2, N1+N2

i don't know how to read X1,N1,X2,N2 where X1 and X2 are strings, i've also tried but problem is that when i read first string it reads entire line for eg when i read string it supposed to be X1 but it is X1,N1,X2,N2. code that i used is

scanf("%s,%d,%s,%d", x1, &n1, x2, &n2)

thanks in advance and sorry for my bad english.

Update#1:

example lines :

3
some,123,thing,456
something,579
a,1,b,2
ab,3
hello,100,world,100
helloworld,200
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Mohan Sharma
  • 180
  • 7

2 Answers2

2

I think you are looking for something like this:

int number_of_inputs;
std::cin >> number_of_inputs;
for (int iteration = 0; iteration < number_of_inputs; ++iteration){
    int integer1, integer2;
    string string1, string2, stupid_comma;
    std::cin >> string1 >> stupid_comma >> integer1 >> stupid_comma >> string2 >> stupid_comma >> integer2;
    std::cout << string1 << " + " << string2 << " = " << integer1+integer2 << std::endl; 
}

edit2: After op provides input, my code is not correct. Check this answer: Parsing a comma-delimited std::string

edit3: alternative split method op requires:

std::vector<std::string> split(const std::string &text, char sep, int num)
{
    std::vector<std::string> tokens;
    std::size_t start = 0, end = 0;
    int elements = 0;

    while ((end = text.find(sep, start)) != std::string::npos) {
        if ( elements == num) break;

        tokens.push_back(text.substr(start, end - start));
        start = end + 1;
        elements++;
    }

    tokens.push_back(text.substr(start));
    return tokens;
}

edit4: new code using split function:

int number_of_inputs;
std::cin >> number_of_inputs;
for (int iteration = 0; iteration < number_of_inputs; ++iteration){
    std:string line;
    cin >> line;
    int integer1, integer2;
    string string1, string2, stupid_comma;
    std::vector<std::string> my_line = split(line, ',', 4);
    string1 = my_line[0];
    string2 = my_line[2];
    integer1 = stoll(my_line[1], nullptr, 10);
    integer2 = stoll(my_line[3], nullptr, 10);
    std::cout << string1 << " + " << string2 << " = " << integer1+integer2 << std::endl; 
}
Community
  • 1
  • 1
tknbr
  • 139
  • 1
  • 13
1

Here is a solution using std::regex, even though it is longer than the accepted answer, I find it much clearer and more flexible.

#include <iostream>
#include <string>
#include <regex>

using namespace std;

struct MyPair {
    string x; int n;

    MyPair() {}
    MyPair( const string& x, int n )
        : x(x), n(n) {}
};

void parse_line( const string& str, MyPair& p1, MyPair& p2 ) {

    typedef regex_iterator<string::const_iterator> re_iterator;
    typedef re_iterator::value_type re_iterated;

    regex re("(\\w+),(\\d+),(\\w+),(\\d+)");
    re_iterator rit( str.begin(), str.end(), re );
    re_iterator rend;

    if ( rit != rend ) {
        p1 = MyPair( (*rit)[1], stoi((*rit)[2]) );
        p2 = MyPair( (*rit)[3], stoi((*rit)[4]) );
    }

}

int main() {

    int A = 0;
    while ( A <= 0 ) {
        cin >> A;
    }

    string line;
    MyPair p1, p2;

    for ( int i = 0; i < A; i++ ) {
        cin >> line;
        parse_line( line, p1, p2 );
        cout << (p1.x + p2.x) << " " << (p1.n + p2.n) << endl;
    }

}

Note that it uses features from C++11, so in order to compile it using clang++ (for instance), you should do:

clang++ -std=c++11 file.cpp -o file
Jonathan H
  • 7,591
  • 5
  • 47
  • 80
  • Thank you, i always learn different things from different answer, so it doesn't matter how long is your code while learning. What i learn from your code is how to use regex in c++. so thanks. but i'm not able grasp first loop in main function. whats the purpose of that loop in main ? – Mohan Sharma Apr 04 '17 at 14:28
  • ohh gotcha.. you're trying to repeat asking procedure until A>0. got it. – Mohan Sharma Apr 04 '17 at 14:29