Write a Java program to input a sentence and print those word which do not contain any vowels and also count those words
Question: Write a Java program to input a sentence and print those word which does not contain any vowels and also count those words.
Welcome to Programming Chaska !!!. In this post, I will share the code to input a sentence and print those word which does not contain any vowels and also count those words. This question is recently asked in google question hub, so in this post, you will get its solution.
Sample Input 1:
Enter Sentence: I am seeing a post from the Programming Questions blog.
Sample Output 1:
All the words in the above sentence contain vowels
Number of words which does not contain any vowel = 0
Sample Input 2:
Enter Sentence: The rhythm and flow of this song are quite decent.
Sample Output 2:
rhythm
Number of words which does not contain any vowel = 1
Solution code:
import java.util.Scanner;
public class consonantWords {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Sentence:");
String sentence = in.nextLine();
in.close();
sentence = sentence + ' ';
sentence = sentence.toLowerCase();
int length = sentence.length();
String word = "";
int count = 0;
boolean consonant = false;
for (int i = 0; i < length; i++) {
if (sentence.charAt(i) == ' ' || sentence.charAt(i) == ',') {
boolean flag = true;
for (int j = 0; j < word.length(); j++) {
char c = word.charAt(j);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
flag = false;
}
if (flag == true) {
System.out.println(word);
count++;
consonant = true;
}
word = "";
} else {
word = word + sentence.charAt(i);
}
}
if (!consonant)
System.out.println("All the words in the above sentence contain vowels");
System.out.println("Number of words which does not contain any vowel = " + count);
}
}
import java.util.Scanner;public class consonantWords {public static void main(String[] args) {Scanner in = new Scanner(System.in);System.out.print("Enter Sentence:");String sentence = in.nextLine();in.close();sentence = sentence + ' ';sentence = sentence.toLowerCase();int length = sentence.length();String word = "";int count = 0;boolean consonant = false;for (int i = 0; i < length; i++) {if (sentence.charAt(i) == ' ' || sentence.charAt(i) == ',') {boolean flag = true;for (int j = 0; j < word.length(); j++) {char c = word.charAt(j);if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')flag = false;}if (flag == true) {System.out.println(word);count++;consonant = true;}word = "";} else {word = word + sentence.charAt(i);}}if (!consonant)System.out.println("All the words in the above sentence contain vowels");System.out.println("Number of words which does not contain any vowel = " + count);}}
I hope that this answer will help you. If you have any question then do not keep it in your mind, put in front of all so that everyone could get its answer.
If you have any questions, feel free to comment below. Subscribe to this blog to get the answers to the latest programming question. You should check my other programming blog too.
Programming Chaska - where you can get several programs in C, C++, and JAVA
Programming Chaska - where you can get several programs in C, C++, and JAVA
Comments
Post a Comment