954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

splitting a string sentence into words

Can someone show me a sample code in c which can read in a user inputted sentence/line and then split that line down by white spaces into separate words please? I shall worship you as my new God if someone out there can just show me a sample solution.

SIFA
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Pretty simple to code this up, why not try?

while there are more letters in the string 
  take each letter and put it into a char words[50][50], incrementing j,
  unless it's a space or punctuation char. (use ctype.h and isalnum() ). 
  In that case, increment i and start a new word[i][j] at the next isalnum()
  char.
end of while

You know this is painful to not give out code! I always wanted to be a God. ;)

You wouldn't learn much that way, though. Doesn't take a God to figure that out.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

By default, operations such as scanf split on whitespace. All that remains is to be able to not overflow buffers and to recognize when the stream has reached an invalid (or empty) state. What exactly are you having trouble with?

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: