Ask Question
10 October, 03:41

Write a program in Java that reads in text and prints as outputthe following:The number of words in the textThe number of sentences in the textThe number of times the letter "e" occurs in thetextThe number of times the letter "z" occurs in thetext

+2
Answers (1)
  1. 10 October, 05:04
    0
    See Below explanation with comments

    Explanation:

    public class HelloWorld {

    public static void main (String[] args) {

    //take any string as your text

    String text = "This is a sample text. Change your text here.";

    //Split it with space, that will gives all the words as an array of String

    String[] words = text. split (" ");

    //initialize variable with zero

    int eCount = 0;

    int zCount = 0;

    int sentenceCount = 0;

    / / iterate words array and increment the count

    for (String word : words) {

    //if a words ends with dot (.), means its end of sentence, increase sentence count

    if (word. endsWith (".")) {

    sentenceCount++;

    }

    / / here again iterate word to get every letter from word

    for (int i = 0; i
    //if its leeter e, increase eCount

    if (word. charAt (i) = = 'e') {

    eCount++;

    //if its leeter z, increase zCount

    }else if (word. charAt (i) = = 'e') {

    zCount++;

    }

    }

    }

    System. out. println ("Number of words in the text : "+words. length);

    System. out. println ("Number of sentence in the text : "+sentenceCount);

    System. out. println ("The number of times the letter / "e/" occurs in the text : "+eCount);

    System. out. println ("The number of times the letter / "z/" occurs in the text : "+zCount);

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program in Java that reads in text and prints as outputthe following:The number of words in the textThe number of sentences in the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers