Wednesday, 12 August 2020

Java program for words which do not contain any vowels in a sentence

How to write a java program words which do not contain any vowels in a sentence using java

 
Here we are going to see a java program to input a sentence and print those word which
  do not contain any vowels
and simultaneously count and print those words.  
 

  •   First read a sentence
  •   Now using StringTokenizer get each word
  •   Now from each word check word contains any vowel or not 


import java.util.*;
class Highest{
 public static void main(String args[]){
    
     //read a sentence
     System.out.println("enter a sentence");
     Scanner sc=new Scanner(System.in);
     String sentence=sc.nextLine();
     StringTokenizer st=new StringTokenizer(sentence);
     String word;
 
     int index=-1,count=0;
     char vowel[]={'a','e','i','o','u'};
     System.out.println("Words which do not contain vowels are ");


     // iterating each word
     while(st.hasMoreTokens()){
        word=st.nextToken();


        // check word contains any vowel or not        


        for(char c:vowel){
            index=-1;
            index = word.indexOf(c);
            if(index>=0) break;
        }
        if(index==-1){
            System.out.println(word);
            count++;
        }

    
     }//while closing
     System.out.println("Total number of words which do not contain vowels are "+count);
    }
 }

No comments:

Post a Comment