splitting numbers and letters from string

Given an input string consisting of the first one or two characters followed by a number, I would like to split it into a characters and an integer. What is the easiest way to accomplish this?

figured i could use something like this below, but what if the user only puts one character or maybe 4 numbers?

1
2
  string mystring = "AJ900";
  string sub = mystring.substr(0, 1);
closed account (LA48b7Xj)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string mystring = "AJ900";
    string letters;
    string number_string;

    for(auto e : mystring)
    {
        if(isalpha(e))
            letters.push_back(e);
        else if(isdigit(e))
            number_string.push_back(e);
    }

    stringstream ss;
    ss << number_string;

    int number;
    ss >> number;

    cout << letters << number << '\n';
}



Topic archived. No new replies allowed.