Text Processing and More about Wrapper Classes 1, Word Counter Write a method th
ID: 3546430 • Letter: T
Question
Text Processing and More about Wrapper Classes
1, Word Counter
Write a method that accepts a String object as an argument and returns the number of words it contains. for instance , if the argument is "Four score and seven years ago" the method shuold return the number 6. demonstrate the method in a program that asks the user to input a string and then passes it to the method. The number of words in the string should be displayed on the screen.
2, Word Separator
Write a program that accepts as sentence in which all of the words are run together, but the first character of each word is uppercase. convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRoses." would be converted to "Stop and smell the roses."
Explanation / Answer
2)String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
s = in.nextLine();
StringBuilder sb = new StringBuilder(s);
for (int index = 0; index < sb.length(); index++) {
char c = sb.charAt(index);
if (Character.isUpperCase(c)) {
sb.setCharAt(index, Character.toLowerCase(c));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.