Knowee
Questions
Features
Study Tools

Raghu's tutor provides him a simple sentence and asks him to count the number of words in the sentence. The words are separated by space.Example, if the tutor provides the sentence as "Welcome to Java", the output should beNumber of words 3The words are WelcometoJavaFill the space in the below code so that the code fulfills his requirement. Code :                Scanner sc=new Scanner(System.in); String str=sc.nextLine(); String words[ ]  =   ; int count=  ;  //count holds the number of words in the sentence System.out.println("Number of words "+); System.out.println("The words are "); for(String    :     ) //for each loop System.out.println(x);

Question

Raghu's tutor provides him a simple sentence and asks him to count the number of words in the sentence. The words are separated by space.Example, if the tutor provides the sentence as "Welcome to Java", the output should beNumber of words 3The words are WelcometoJavaFill the space in the below code so that the code fulfills his requirement. Code :                Scanner sc=new Scanner(System.in); String str=sc.nextLine(); String words[ ]  =   ; int count=  ;  //count holds the number of words in the sentence System.out.println("Number of words "+); System.out.println("The words are "); for(String    :     ) //for each loop System.out.println(x);

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the completed code:

Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String words[] = str.split(" ");
int count= words.length; //count holds the number of words in the sentence
System.out.println("Number of words "+ count);
System.out.println("The words are ");
for(String x : words) //for each loop
    System.out.println(x);

In this code, we are using the split method of the String class to split the input string into an array of words. The split method takes a regular expression as a parameter, and in this case, we are using a space (" ") as the delimiter. The length property of the array gives us the number of words in the sentence. We then print the count and each word in the sentence using a for-each loop.

This problem has been solved

Similar Questions

Tom is playing scrabble and forms new words. He wants to count the number of characters present in the word formed by him. Help him to do this using the below java program.Code :import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); //find length and print the same          int len=  ;                System.out.println("The length of the word is "+)  ; }}

Select the correct implementation of a piece of code that accepts a sentence as input and prints the number of words in it. A sentence is just a sequence of words with a space between consecutive words. You can assume that the sentence will not have any other punctuation marks. You can also assume that the input string will have at least one word.1sentence = input()2space = ' ' # there is a single space between the quotes3num_words = sentence.count(space)4print(num_words)1sentence = input()2space = ' ' # there is a single space between the quotes3num_words = sentence.count(space) + 14print(num_words)1sentence = int(input())2space = ' ' # there is a single space between the quotes3num_words = sentence.count(space)4print(num_words)1sentence = input()2num_words = len(sentence)3print(num_words)

Write a java program to accept a String and the perform the following operations:a) Count the number of words with the first and the last letter being same.

Write a program to count number of words in a given string whose length is greater than 3.For example:Input Resultpython is easier than C3

Problem StatementWrite a program to count the lines, words and characters in a given text.Input FormatInput the text (Ctrl+D on a new line to end input)Output FormatPrint the no of lines, words, charactersSample Input 1This is a sample text.It has multiple lines.Each line contains words.Sample Output 1Lines: 3Words: 12Characters: 80Sample Input 2Hello WorldHappy CodingSample OutputLines: 2Words: 4Characters: 23

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.