Computer Science, asked by dhoni7627, 4 months ago

Write a program to input a sentence. Find and display the following:
(i) Number of words present in the sentence
(ii) Number of letters present in the sentence
Assume that the sentence has neither include any digit nor a special character.​

Answers

Answered by Oreki
1

import java.util.StringTokenizer;

import java.util.Scanner;

public class SentenceAnalyzer {

   public static void main(String[ ] args) {

       System.out.print("Enter a sentence - ");

       String sentence = new Scanner(System.in).nextLine( );

       int wordCount = new StringTokenizer(sentence).countTokens( ),

             // Replacing all whitespaces with blank character and the counting the remaining letters.

             letterCount = sentence.replaceAll("\\W", "").length( );

       System.out.println("Number of words - " + wordCount);

       System.out.println("Number of letters - " + letterCount);

   }

}

Sample I/O:

   Enter a sentence - Java is easy

   Number of words - 3

   Number of letters - 10


anindyaadhikari13: Nice!
Oreki: Thanks!
Similar questions