Tuesday 21 July 2015

Create a class which ask the user to enter a sentence, and it should display count of each vowel type in the sentence. The program should continue till user enters a word “quit”.

  
         
import java.util.Scanner;
public class Program_8 {

    public static void main(String[] args) {

        System.out.println("Enter the word 'quit' to end this program.");
        String str;
        int x = 0, A = 0, E = 0, I = 0, O = 0, U = 0;
        char ch;
        Scanner in = new Scanner(System.in);
        while (x < 1) {
            str = in.next();
            if (str.equals("quit")) {
                x++;
                in.close();
                break;
            } else {
                for (int i = 0; i < str.length(); i++) {
                    ch = str.charAt(i);

                    if (ch == 'a' || ch == 'A') {
                        A++;
                    } else if (ch == 'e' || ch == 'E') {
                        E++;
                    } else if (ch == 'i' || ch == 'I') {
                        I++;
                    } else if (ch == 'o' || ch == 'O') {
                        O++;
                    } else if (ch == 'u' || ch == 'U') {
                        U++;
                    }
                }
            }
        }
        System.out.println("Vowels A or a: " + A + "\nVowels E or e: " + E + "\nVowels I or i: " + I);
        System.out.println("Vowels O or o: " + O + "\nVowels U or u: " + U + "\nTotal Vowels: " + (A + E + I + O + U));
    }
}
        
  

2 comments:

  1. import java.util.*;

    class Countvowel
    {
    public static int a,e,i,o,u;

    public static void main(String m[])
    {

    Scanner in=new Scanner(System.in);
    String s=new String();
    while(true)
    {
    int ta=0,te=0,ti=0,to=0,tu=0;
    System.out.println("Enter A line :");
    s=in.nextLine();
    if(s.equals("quit"))
    {
    break;
    }
    else
    {
    int n=s.length()-1;
    for(int x=0;x<=n;x++)
    {
    switch(s.charAt(x))
    {
    case 'a' : case 'A' : a++; ta++; break;
    case 'e' : case 'E' : e++; te++;break;
    case 'i' : case 'I' : i++; ti++; break;
    case 'o' : case 'O' : o++; to++; break;
    case 'u' : case 'U' : u++; tu++; break;
    }

    }
    System.out.println("\n\n In this statement:");
    System.out.println("a comes: "+ta+" times");
    System.out.println("e comes :"+te+" times");
    System.out.println("i comes :"+ti+" times");
    System.out.println("o comes: "+to+" times");
    System.out.println("u comes :"+tu+" times");
    }
    }
    System.out.println("\n\n Totle numbers of the vovels::");
    System.out.println("a comes "+a+" times");
    System.out.println("e comes "+e+" times");
    System.out.println("i comes "+i+" times");
    System.out.println("o comes "+o+" times");
    System.out.println("u comes "+u+" times");
    }
    }

    ReplyDelete
    Replies
    1. Thank you for Comment ....
      now it resolved

      Delete