I need to write a program using just strings. The problem I am having is figuring out which strings I need to use.
I have to prompt the user for a sentence that contains 4 words. That's easy, I know how to do that.
The problem I having is with what you do with the sentence.
Print the number of letters in each sentence
I would use str.length();
Print the average word length
I have no idea what string to use. I looked through the examples I have and I've never had an example where I've found the average number of words?
Print each word on a separate line
Again, I've never been shown an example to know what string to use.
Print the sentence with the words reversed.
I've seen several strings that have been used to reverse the sentence, but none that I've ever had an example for and I don't know how to use the ones that I found.
I just need help with what strings to use. I don't need the program written, I just have no idea what strings should be used to carry out the program.
Thanks in advance.
In a sentence, strings are usually separated by spaces (or whitespaces). To find where one word ends and another starts look through the sentence string looking for white spaces.
To look a specific character in a string, use the charAt(int loc) method. To extract a word (sub-string) from the sentence, use the substring(int start, int end) method.
To print out the words in reverse order (without reversing the order of their characters) just print out the last word you found first and the first word you found last.
To reverse the letters there's a topic in the tips section about reversing strings.