Determining String Lengths and Word Counts

You can determine not only the length of a string, but the number of words within the string, or the length of a particular word in the string. The length of a string is not affected by any null characters you insert in the middle of the string. (The strlen function of string.h treats any null character in an array of characters as a terminating null.)

The following descriptions assume that ThisString contains the text "This string has five words".

The length and size functions both return the length of an IString. For example, ThisString.size() returns the value 26, as does ThisString.length().

To determine the number of words in a string, use the numWords member function. For example, ThisString.numWords() returns the value 5.

To determine the length of a particular word, use the lengthOfWord member function. For example, ThisString.lengthOfWord(3) returns the value 3.