Computer Science, asked by tamalika2004, 11 months ago

write a program to input a sentence and find the number of words and letters present in the sentence​

Answers

Answered by TANSH9KANSARA
6

Answer:

In JAVA

import java.util.Scanner;

public class ToCountNumberOfWords{

public static void main(String[] args)

{

 

 Scanner T=new Scanner(System.in);

 System.out.println("Enter a sentence!");

 

 while(T.hasNext())

 {

   

  String line=T.nextLine();

  /* this string will contain the words that are split or we can say break by the split function

  split function takes parameter i.e from where we have to split the string

  here,that parameter is a space as soon as it will find a space,it will break that into substring from there

  and store them in string array arr with an index  

  */

  String[] arr=line.split(" ");

  //counter to count the number of words initialized to zero

  int word=0;

  //looping to count the number of words in array  

  for(int i=0;i<arr.length;i++)

{

   word++;

  }  

  //print the number of words in a sentence

  System.out.println(word);

 }

}

}

I hope you find this answer helpful. Mark it as branliest. And click that red or pink button...;-)  

Similar questions