Make a program so that reads a full line of \"Scanner\" input, then outputs the
ID: 3619548 • Letter: M
Question
Make a program so that reads a full line of "Scanner" input, then outputs the following:a) The length of the string
b) The number of words in the string (hint: look for spaces in the string)
c) Every word in the string on its own line
Remember the String methods length and charAt
REMEMBER: By "words" I do not mean actual words "Hello , Yes , No etc...."-- Words are numbers , letters , and special characters "67 , '[;'; , absjd" are all words. EXAMPLE:
This is a string input "asd 213 ['/3"
The program should output the length of the string is 12 characters. (including spaces)
The program should also output the number of words in the string: 3. The program should output each string on a line.
REMEMBER: By "words" I do not mean actual words "Hello , Yes , No etc...."-- Words are numbers , letters , and special characters "67 , '[;'; , absjd" are all words. EXAMPLE:
The program should also output the number of words in the string: 3. The program should output each string on a line.
Explanation / Answer
please rate - thanks import java.util.*;public class wordcount{
public static void main(String[] args)
{String input,word="";
int len=0,words=0,i;
char ch;
Scanner in=new Scanner(System.in);
input=in.nextLine();
len=input.length();
System.out.println("length of the string: "+len);
System.out.println("The words in the string are:");
for(i=0;i<len;i++)
{ch=input.charAt(i);
if(ch==' ')
{System.out.println(word);
words++;
word="";
}
else
word+=ch;
}
System.out.println(word);
words++;
System.out.println("number of words: "+words);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.