[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

Program Discussion :: Basics

Home > Programs > Basics

7 / 279

Write a program to to count number of vowels and consonants in a string.

Answer:

import java.io.*;
class q5vowels
{
    public static void main(String args[]) throws IOException
    {
        String str;
        int vowels = 0, digits = 0, blanks = 0;
        char ch;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter a String : ");
        str = br.readLine();

        for(int i = 0; i < str.length(); i++ )
        {
            ch = str.charAt(i);

            if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || 
            ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
                vowels++ ;
            else if(Character.isDigit(ch))
                digits++ ;
            else if(Character.isWhitespace(ch))
                blanks++ ;
        }

        System.out.println("Vowels : "+ vowels);
        System.out.println("Digits : "+ digits);
        System.out.println("Blanks : "+ blanks);
    }
}

Asked In :: IBM

Post Your Answer Here:

Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here: