1

We have a string (25;16;67;13;14;.......)

We need to print out the numbers separately. The last number does not have a semicolon behind it.

Output should be something like that:

25
16
67
13
14
......

Assuming we are using str.find, str.substr and size_t variables current_pos, prev_pos, what will be the condition of the while loop we are using to browse the line, so that it prints out all the numbers, not just the first one?

  • 2
    Does this answer your question? [How do I tokenize a string in C++?](https://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c) – Robert Prévost Mar 14 '20 at 16:13

4 Answers4

1

You can make use of std::istringstream:

#include <sstream>
#include <iostream>

int main() {

    std::string text("25;16;67;13;14");
    std::istringstream ss(text); 
    std::string token;

    while(std::getline(ss, token, ';'))
    {
        std::cout << token << '\n';
    }

    return 0;
}

Running the above code online results in the following output:

25
16
67
13
14
BlueTune
  • 1,023
  • 6
  • 18
1

If you need only to print the numbers in the string (rather than represent them in data structures) the solution is quite easy. Simply read the entire string, then print it character by character. If the character is a semicolon, print a new line instead.

#include <iostream>
#include <string>

using namespace std;

int main(){
    string input;
    cin >> input;
    for(int i = 0; i < input.length(); i++){
        if(input.at(i) == ';') cout << endl;
        else cout << input.at(i);
    }
}
Ross Douglas
  • 120
  • 1
  • 5
1
using namespace std;
int main() {
string a{ "1232,12312;21414:231;23231;22" };
for (int i = 0; i < a.size(); i++) {
    if (ispunct(a[i])) {
        a[i] = ' ';
    }
}
stringstream line(a);
string b;
while (getline(line, b, ' ')) {
    cout << b << endl;

}

} //any punctuation ",/;:<>="

0

I will give you an exact answer to your question with an example and an alternative solution with an one-liner.

Please see

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <regex>

const std::regex re(";");

int main() {

    std::string test("25;16;67;13;14;15");

    // Solution 1: as requested
    {
        size_t current_pos{};
        size_t prev_pos{};
        // Search for the next semicolon
        while ((current_pos = test.find(';', prev_pos)) != std::string::npos) {

            // Print the resulting value
            std::cout << test.substr(prev_pos, current_pos - prev_pos) << "\n";
            // Update search positions
            prev_pos = current_pos + 1;
        }
        // Since there is no ; at the end, we print the last number manually
        std::cout << test.substr(prev_pos) << "\n\n";
    }

    // Solution 2. All in one statement. Just to show to you what can be done with C++
    {
        std::copy(std::sregex_token_iterator(test.begin(), test.end(), re, -1), {}, std::ostream_iterator<std::string>(std::cout, "\n"));
    }
    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44