Click here to Skip to main content
15,889,027 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have to make a code that read a line strings from user and divides this string when there is a space, store and then do some operations with this input.

For example the input can be like That :

"set 11643133 name Alex Jon"
all this it have to be input in a one line, and I have to divide it for set a name "Alex Jon" to the student's ID "11643136"

I really have to hand the project in nearest time, I just want to ask if there is a simple way or some standard commands maybe can it helps me at that ?

*I use getstr() and string.substr()

What I have tried:

C++
getstr();
string.substr;
Posted
Updated 24-Oct-17 12:29pm
v2
Comments
PIEBALDconsult 24-Oct-17 18:25pm    
strtok maybe?

1 solution

You could save the input into a std::string and use find_first_of to look for spaces or just iterate through the characters in the string and use a simple state machine to tell you what to do with them (either saving them or stepping through spaces.)

You could use standard C functions like strtok to do this also. That depends on the requirements for your homework.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900