Featured Posts

Most selected posts are waiting for you. Check this out

Java program to display the number of vowels in each word of the Sentence

Java program to display the number of vowels in each word of the Sentence

Java program to display the number of vowels in each word of the Sentence

 

Java program to display the number of vowels in each word of the Sentence

 In the following the java program, we calculate vowels of each word of a sentence, entered by the user. The sentence is taken in the st variable. After that word will be saved in word variable in the following block

        for(i=0; i<l; i++)

        {

            ch=st.charAt(i);

            // if space is come then goto else calculate each words vowel

            if(ch!=' ')

            {

                word=word + ch;

            }

The word is build by each character taken till space is encounter. And then count the vowels from the word and print.

 

import java.util.*;

public class EachWordsVowelPrint

{

    public static void main(String args[])

    {

        String st, word="";

        char ch,ch1;

        int v;      // variable for vowel

        int c;      // variable for Consonents

        int w;      // variable for word

        int i,j;    // for loop

        int l,l1;   // l is for length of sentence and for word

        v=c=w=0;    // Initialization

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a sentence:");

        st=sc.nextLine();

        st=st + " ";    // one space add to the sentence

        //st=st.toUpperCase();

        l=st.length();  // length of the sentence

        for(i=0; i<l; i++)

        {

            ch=st.charAt(i);

            // if space is come then goto else calculate each words vowel

            if(ch!=' ')

            {

                word=word + ch;

            }

            else

            {

                l1=((word.trim()).length());   // length of the word               

                for(j=0; j<l1; j++)

                {

                    ch1=word.charAt(j);                    

                    switch(ch1)

                    {

                        case 'A' :

                        case 'a' :

                        case 'E' :

                        case 'e' :

                        case 'I' :

                        case 'i' :

                        case 'O' :

                        case 'o' :

                        case 'U' :

                        case 'u' :

                        v++; 

                    }                 

                }

                // print the word total vowels

                System.out.println(word + " contain vowel= " + v);

                // Initialize the words and v for vowel

                word = "";   

                v=0;

            }

        }

    }

}

Output

Enter a sentence:

the quick brown fox jumps over a lazy hungry dog

the contain vowel= 1

quick contain vowel= 2

brown contain vowel= 1

fox contain vowel= 1

jumps contain vowel= 1

over contain vowel= 2

a contain vowel= 1

lazy contain vowel= 1

hungry contain vowel= 1

dog contain vowel= 1

 

More Java programs 


Pascal triangle  in java

Pascal triangle in java

Pascal triangle  in java

 Pascal Triangle in java

The pattern similar to Floyed triangle, there is another triangle is called Pascal triangle named after famous French mathematician Blaise Pascal. But it has a little bit different. Here each row is the summation of the left value and a right value of the previous row. if the above row value is missing then it is taken as 0.

The formula is co = co * (i - j + 1) / j Where i is the row number and j is the element position in that row.

 

Java program for Pascal triangle

import java.util.*;

public class PascalTriangle

{

    public static void main(String args[])

    {

        int rows, co, i, j;

        co = 1;

        Scanner sc = new Scanner (System.in);

        System.out.println("Enter the number of rows");

        rows = sc.nextInt();  

        for(i = 0; i < rows; i++)

        {

            // print the space

            for(j = 1; j < rows - i; j++)

            {

                System.out.print(" ");

            }

            // print value

            for(j = 0; j <= i; j++)

            {

                if (j == 0 || i == 0)

                {

                    co = 1;

                }

                else

                {

                    co = co * (i - j + 1)  /  j;

                }

                // row print

                System.out.print(co + " ");

            }

            System.out.println();

        }

    } // end of the main()  method

} // end of the class

Output

Enter the number of rows

6

      1

     1 1

    1 2 1

   1 3 3 1

  1 4 6 4 1

1 5 10 10 5 1

Java program for Pascal triangle with recursive method

 In this program, Pascal triangle is generated with recursive method pascalTriangle(int i, int j).

 

import java.util.*;

public class PascalTriangleRecursiveMethod

{  

    //instance variable;

    int i, j, k;

    public void printRow(int n)

    {

        for (i = 0; i < n; i++)

        {

            for (k = 0; k < n - i; k++)

            {

                // print the space

                System.out.print(" ");

            }

            for (j = 0; j <= i; j++)

            {

                System.out.print(pascalTriangle(i, j) + " ");

            }

            System.out.println();

        }

    }

 

    public int pascalTriangle(int i, int j)

    {

        if (j == 0 || j == i)

        {

            return 1;

        }

        else

 

            return pascalTriangle(i - 1, j - 1) + pascalTriangle(i - 1, j);

    }

 

    public static void main(String[] args)

    {

        int rows;

        PascalTriangleRecursiveMethod ob = new PascalTriangleRecursiveMethod();

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of rows : ");

        rows = sc.nextInt();

        System.out.println("Pascal Triangle printed as:");

        System.out.println("======================”);

        ob.printRow(rows);

    }

}

Output

Enter the number of rows: 5

Pascal Triangle printed as:

======================

     1

    1 1

   1 2 1

  1 3 3 1

 1 4 6 4 1

More Java programs