By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

How do you split a string of multiple words into single words

P: n/a
What's the best method to split a string variable that holds someones full name (ex John Smith) and put the first name and last name into separate string variables? I know that searching for the space is the best way to start I'm just not sure how to go about it.
Sep 29 '10 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
In C++ use a string stream and just pipe the data out of the string.

In C you could use strtok but it is a really awful function that keeps static and is not at all thread safe so I would be inclined just to do it through pointer manipulation.
Sep 30 '10 #2

P: 3
find the first space and store the word up to the space in to a string of name 'first name' and continue the process untill '\0' reach
Oct 1 '10 #3

Expert 100+
P: 2,396
Is the input string read-only?
Do you need this input string for anything else?

If the answer to both questions is NO, then you can change the input string into two strings by replacing the first whitespace character between the first and last names with null ('\0') and return two pointers into the original string -- one to the first character of the first name and the other to the first character of the last name.
Oct 1 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.