Answer to Question #531 in C++ for Angelo

Question #531
How to break the string of characters into words?
1
Expert's answer
2010-08-03T11:26:10-0400
If you have a string of characters of type char* and you need to break it up into words, you can:

1. Create an array of pointers of the type char*. Create a word counter.
2. Save the current value of string length. Assign the first pointer to the address of the beginning of the string. Increase counter value.
3. In a cycle from zero to the length of the string replace all the non-letters with the symbol “\0” (end of line character). (You can check if the symbol is a letter or not with the function isalpha). Save to a pointer array the address of the symbol going after non-letter. Increase the count of the number of words.
4. You must then go through the formed array and remove empty words from there.

Note: the word count is useful for indexing the pointer in an array of pointers. For example words[n]=&str[i]; Since words in C have the ending “\0”, you will have a set of strings that will be kept in the same place as before, but it will be possible to work with them as with the separate strings.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS