Reading a String [closed]

-2

If I have a String that is:

String s = "2123dog";

How can I calculate the first character 2 to transform the next 2 characters into an integer, and 4 to transform the name that is dog?

The result would be (12, dog)?

    
asked by anonymous 30.07.2018 / 02:24

1 answer

0

I do not know if I understood your question correctly, but maybe it was something like this:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "2123dog";
    int n = stoi(s.substr(0, 1), NULL);
    int n2 = stoi(s.substr(1, n), NULL);
    int n3 = stoi(s.substr(1 + n, 2 + n), NULL);
    string resto = s.substr(2 + n);
    cout << n << " - " << n2 << " - " << n3 << " - " << resto;
    return 0;
}

The output is: 2 - 12 - 3 - dog . Notice that all three numbers were stored in variables of type int .

See here working on ideone.

    
30.07.2018 / 02:43