Seperate a string.

Hello, I have to separate a string of unknown length which is in a format that looks like "xxxYxxxYxxxY" where 'x' is a three digit number ranging from 0-9 and 'Y' is a single character letter ranging from A-Z. I was wondering if someone could show me how to do that.

Here is an example string and output:

Input:
012X999Y764Z

Output:
1
2
3
4
5
6
Number = 012
Letter = X
Number = 999
Letter = Y
Number = 764
Letter = Z
Last edited on
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <cassert>
#include <iostream>

void foo( const std::string& str )
{
    assert( str.size()%4 == 0 ) ;

    for( std::size_t pos = 0 ; pos < str.size() - 3 ; pos += 4 )
    {
        // http://en.cppreference.com/w/cpp/string/basic_string/substr
        std::string number = str.substr( pos, 3 ) ;

        char letter = str[pos+3] ;

        std::cout << number << ' ' << letter << '\n' ;
    }
}
Topic archived. No new replies allowed.