write a program in java to input any paragraph and print all the sentences with their number of vowels , consonants and number of words stating with vowels(use different functions to do the above task).
Answers
Program:
import java.util.*;
public class Main{
public static void main(String[] args) {
System.out.println("Enter a paragraph");
Scanner sc = new Scanner(System.in);
String para = sc.nextLine();
String[] words = para.split("\\s");
int v=0,c=0,w=0;
for(String m:words){
for(int i = 0 ; i < m.length() ; i++){
if(m. charAt(i) == 'a' || m. charAt(i) == 'A' || m. charAt(i) == 'e' || m. charAt(i) == 'E' || m. charAt(i) == 'i' || m. charAt(i) == 'I' || m. charAt(i) == 'o' || m. charAt(i) == 'O' || m. charAt(i) == 'u' || m. charAt(i) == 'U'){
v++;
if(i==0){
w++;
}
}else if( ( m. charAt(i) >= 'a' && m. charAt(i) <= 'z' ) || ( m. charAt(i) >= 'A' && m. charAt(i) <= 'Z' ) ){
c++;
}
}
}
System.out.print("\nvowels are : "+v+"\nConsonants are : "+c+"\nwords start with vowels are : "+w);
}
}
Output:
Enter a paragraph
hello there i have only one reason to stay here
vowels are : 17
Consonants are : 21
words start with vowels are :
Explanation:
- first i took paragraph into para variable of string type
- then i splitted the para into words by giving "\\s" to the predefined method call split()
- "\\s" means white space so the whole para string is splitted using white space
- and the i stored each string into a string array "words" in which we have all the words from a para string
- i initialized for loop as (String m:words) which will select each element per iteration
- so i took the element which is string which is a word and check the vowels and consonants in it using pre-defined method called charAt() which will takes number as argument and returns the character at that particular index
- so i check each and every letter of the word and i incremented v++ if ti is vowel or incremented c++ if it is consonant and incremented w++ if the word is starting with a vowel
- so at last i printed those values
---Hope you liked my program, if you liked it mark as brainliest,it would really help me. :)