Showing posts with label Java program to print the words starting with a Consonant. Show all posts
Showing posts with label Java program to print the words starting with a Consonant. Show all posts

Java program to print the words starting with a Consonant | JAVA POINT

Java program to print the words starting with a Consonant


Write a Java program to enter a sentence and print the words starting with a Consonant


PROGRAM :


import java.util.*;

class print_consonant

{

   public static void main(String args[])

   {

        Scanner sc=new Scanner(System.in);            

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

        String s=sc.nextLine();

        s = s+" ";

        int l=s.length();

        int pos=0;

        char ch1, ch2;

        String w;

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

        {

            ch1 = s.charAt(i);

            if(ch1 == ' ')

            {

                w = s.substring(pos,i); // extracting words one by one

                ch2 = w.charAt(0);

                if(ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U' || ch2=='a' || ch2=='e' || ch2=='i' || ch2=='o' || ch2=='u')

          pos = i+1;                    

                else

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

          pos = i+1;

            }

        }

   }

}


/*

 Output :

 Enter any sentence: He is a boy

 He boy

*/

Java program to Check the Disarium Number