Featured Posts

Most selected posts are waiting for you. Check this out

Java program for vowels of each word of a sentence

Java program for vowels of each word of a sentence

Java program for vowels of each words of a sentence


Java program for vowels of each word of a sentence

 The following program will print each word’s total vowels. In the following program we have taken a sentence and then first segregate each word. After then each word's vowels is calculated in the loop.

import java.util.*;

public class Sent2Word

{

    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 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.length();   // length of the word

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

                {

                    ch1=st.charAt(j);

                    if(ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='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 dog

THE contain vowel=1

QUICK contain vowel=1

BROWN contain vowel=1

DOG contain vowel=1


More Java programs 

 

Binary search in java with bubble sort

Binary search in java with bubble sort

Binary Search in java with Bubble sort

Binary search in java with bubble sort 

Binary Search with Bubble sort program has been defining as follows. First, we have taken 10 elements in the array name[] and then perform the bubble sort. After sorting we have done binary sort.

What is Binary search?

Binary search is a search algorithm that is faster than linear search and the time the complexity of the above algorithm is O(n). In this search, data must be in sorted order and it perform divide the array length by 2 and find the middle value and if the middle value less than the search value then it performs towards array lesser value till it finds the match and if the search element greater the middle value then it performs a search towards the array’s upper part of its mid value.

Java program is as follows   

import java.io.*;

public class BinarySearchWithSorting

{

    public static void main(String args[]) throws IOException

    {

        String name[] = new String[20];

        String s, t;

        int i,f=0,j;

        int low, high, mid, pos;

        InputStreamReader in=new InputStreamReader(System.in);

        BufferedReader br=new BufferedReader(in);

        System.out.println("Enter 10 Names in array:");

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

        {

            name[i] = br.readLine();

        }

        System.out.println("Enter name to Search:");

        s = br.readLine();

        // Bubble Sorting of array

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

        {

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

            {

                if(name[j].compareTo(name[j+1])>0)

                {

                    t = name[j];

                    name[j] = name[j+1];

                    name[j+1] = t;

                }

            }

        } // end of sorting

        System.out.println ("Sorted array :");

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

        {

            System.out.print (name[i] + " ");

        }

        // Binary Search

        high = 10;

        low = 0;

        pos = 0;

        while(low <= high)

        {

            mid = (low + high) / 2;

            if(s.compareTo(name[mid])<0)

            {

                high = mid - 1;

            }

            else if(s.compareTo(name[mid])>0)

            {

                low = mid + 1;

            }

            else if(s.compareTo(name[mid])==0)

            {

                pos = mid + 1;

                break;

            }

        }

        System.out.println("\n Search result");

        System.out.println(s+" is located at " + pos);

    }

}

Output

Enter 10 Names in the array:

Ramesh

Akbar

Sunil

Binay

Cindy

Umesh

Junaid

Danny

Kalim

Nagma

Enter name to Search:

Sunil

Sorted array:

Akbar Binay Cindy Danny Junaid Kalim Nagma Ramesh Sunil Umesh

 Search result

Sunil is located at 9


More Java programs 

 

Selection sort in java with two arrays

Selection sort in java with two arrays

 

selection sort in java


Selection sort in java with two values (arrays)

Selection sort
In the following program, we have done a selection sort on two arrays, age, and name. Here selection sorting is done by name in ascending order.

public class SelectionSortWithTwoValue

{

    public static void main(String args[])

    {

        int age[]={15,17,20,18,14};  // age array

        String name[]={"Habib","Junaid","Vikash", "Anthony", "Dhoni"}; // name array

        int i, j, temp1;

        String temp2;   // temporary variable for name for swaping data

        System.out.println("Name\t\tAge");

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

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

        {

             System.out.println(name[i] + "\t\t" + age[i]);

        }

// selection sort is started

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

        {

            for(j=i+1;j<5;j++)

            {

                if(name[i].compareTo(name[j])>0)

                {

                    temp2=name[i];

                    name[i]=name[j];

                    name[j]=temp2;

                    temp1=age[i];

                    age[i]=age[j];

                    age[j]=temp1;

                }

            }

        }

// selection sort is ended

        System.out.println("Name\t\tAge");

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

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

        {

             System.out.println(name[i] + "\t\t" + age[i]);

        }

    }

}

Output

Before sorting
Name Age
=====================
Habib 15
Junaid 17
Vikash 20
Anthony 18
Dhoni 14

After sorting
Name Age
=====================
Anthony 18
Dhoni 14
Habib 15
Junaid 17
Vikash 20