1

I have the following string :-

CoursesExams = HUM001,Technical Writing,28/4/2016,HallA;CSE121,Computer Programming,3/5/2016,HallB]

  I want to split it after each ; into an array. How can I do that using c++?

1 Answer 1

1

Use std::getline and stringstream:

std::string s = "HUM001,Technical Writing,28/4/2016,HallA;CSE121,Computer Programming,3/5/2016,HallB]";
std::vector<std::string> arr;
std::istringstream str(s);
std::string elem;

// getline reads str stream until comma is found, then returns string in elem
while(std::getline(str, elem, ',')) arr.push_back(elem);

for (auto& s : arr) std::cout << s << "\n";
Sign up to request clarification or add additional context in comments.

2 Comments

the last lint raise the following error: {a function definition isn't allowed here before ':' token } how can I handle that?
@HossamSalah do you use c++11 compiler? You can rewrite it as for (size_t i = 0; i < arr.size(); ++i) { std::cout << arr[i] << "\n"; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.