Write a program to accept a line and check how many consonants and vowels are there in line

Write a program to accept a line and check how many consonants and vowels are there in line

Explanation :

Hello friends i hope you like my previous post, in today’s post we are going discuss about Write a program to accept a line and check how many consonants and vowels are there in line

so you have to Write a program to accept a line and check how many consonants and vowels are there in line (you have to take string from user)

so now let’s discuss the logic, how you can implement it

First define the variables in main class

  • integer
    • consonants = 0;
    • vowels =0;
  • string
    • str;
  • char
    • ch;

you have to take line from user so you have to make use of Scanner class and store the user input in string variable “str

note : make sure you use of BufferedReader() class for scanning user input

Now make use of for loop to scan all characters of string and run for loop from 0 to length of string so all characters can be covered

[Ex: for(int i = 0; i < str.length(); i ++){ } ]

Now inside for loop take one character variable  “ch” to store the single character from string by passing “for” loop’s integer variable “i” to charAt(i) (string method)

[Ex: ch = str.charAt(i);]

then you have to find whether this character is consonant or vowel so you have to take all possible condition for vowels in if() condition

[ Ex:    if(ch == ‘a’ || ch == ‘A’ || ch == ‘e’ || ch == ‘E’ || ch == ‘i’ || ch == ‘I’ || ch == ‘o’ || ch == ‘O’ || ch == ‘u’ || ch == ‘U’) { vowels ++; } ]

if condition doesn’t match that mean this character is consonant  so we can make use of elseif for identifying consonant 

[ Ex: else if(Character.isAlphabetic(ch)) { consonants ++; } ]

you can see in example every time we identify any consonant or vowel we increment the counter variable “vowels” and “consonants

now you just have to print variables  “vowels” and “consonants” using System.out.println() 

[Ex: System.out.println(“Vowels : ” + vowels);
        System.out.println(“Consonants : ” + consonants); ]

so here your program Write a program to accept a line and check how many consonants and vowels are there in linecompleted

Check Out other post – Write a program to find length of string and print second half of the string

if you like the the way of explanation tell us in comment section you can also write the queries about this program we are defiantly going reply

share this post with your college friends so everyone can get benefit of it good bye friends see you in next post happy coding

Program As per GTU Syllabus

Program :

OUTPUT :

Write a program to find length of string and print second half of the string

 

 

You May Also Like

About the Author: TheCoder

Leave a Reply

Your email address will not be published. Required fields are marked *