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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Borba

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

    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.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • arunkthaomas
      New Member
      • Oct 2010
      • 3

      #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

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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.

        Comment

        Working...