Ask Question
29 September, 13:24

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. 29 September, 15:42
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers