Write a java program that asks the user to enter a sentence

Write a java program that asks the user to enter a sentence. The program should output how many characters are in the sentence, how many letters are in the sentence, the letter with the most frequency in the sentence, how many words are in the sentence, and how many words start with a vowel in the sentence

Solution

Count.java

import java.util.Scanner;


public class Count {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter a sentence: \");
       String s = scan.nextLine();
       int characterCount = s.length();
       int letterCount = 0;
       int frequency = 0;
       int max = 0;
       char letter=\'\\0\';
       int wordCount = 0;
       int vowelWordCount = 0;
       for(int i=0; i<s.length(); i++){
           char ch = s.charAt(i);
           if((ch == \'A\' && ch == \'Z\') || (ch == \'a\' || ch== \'z\')){
               letterCount++;
           }
       }
       for(int i=0; i<s.length(); i++){
           frequency = 0;
           for(int j=0; j<s.length(); j++){
               if(Character.toUpperCase(s.charAt(j)) == Character.toUpperCase(s.charAt(i)))
               frequency++;
           }
           if(max < frequency){
               max = frequency;
               letter = s.charAt(i);
           }
       }
       String words[] = s.split(\"\\\\s+\");
       wordCount = words.length;
       for(int i=0; i<wordCount; i++){
           char ch = words[i].charAt(0);
           if(ch==\'A\' || ch==\'E\' || ch==\'I\' ||ch==\'O\'||ch==\'U\' ||ch==\'a\'||ch==\'e\'||ch==\'i\'||ch==\'o\'|ch==\'u\'){
               vowelWordCount++;
           }
       }
       System.out.println(\"Number of characters are in the sentence: \"+characterCount);
       System.out.println(\"Number of letters are in the sentence: \"+letterCount);
       System.out.println(\"The letter with the most frequency in the sentence: \"+letter);
       System.out.println(\"Number of words are in the sentence: \"+wordCount);
       System.out.println(\"Number of words start with a vowel are in the sentence: \"+vowelWordCount);
      
   }

}

Output:

Enter a sentence:
THis is suresh murapaka
Number of characters are in the sentence: 23
Number of letters are in the sentence: 3
The letter with the most frequency in the sentence: s
Number of words are in the sentence: 4
Number of words start with a vowel are in the sentence: 1

Write a java program that asks the user to enter a sentence. The program should output how many characters are in the sentence, how many letters are in the sent
Write a java program that asks the user to enter a sentence. The program should output how many characters are in the sentence, how many letters are in the sent

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site