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 29 30
|
#include <iostream>
#include <string>
struct person_info
{
std::string name;
std::string gender;
std::string age;
} pinfo;
int main()
{
std::string str = "Olivia Montgomery*female*47;John Newton*male*34;Carla Walton*female*43;";
// name is from position 0, to until first * found
pinfo.name = str.substr(0, str.find("*", 0));
str = str.substr(str.find("*", 0)+1, str.length()); // new string
// gender is from position 0 to until first * found
pinfo.gender = str.substr(0, str.find("*", 0));
str = str.substr(str.find("*", 0)+1, str.length()); // new string
// age is from position 0, to until ";" found
pinfo.age = str.substr(0, str.find(";", 0));
str = str.substr(str.find(";", 0)+1, str.length()); // new str
// prints name, gender, age
std::cout << pinfo.name << ", " << pinfo.gender << ", " << pinfo.age << "\n";
}
|