JAVA Program using Threads to Reverse String and Count Number of Vowel

JAVA Program using Threads to Reverse String and Count Number of Vowel:

import java.io.*;
class rev extends Thread
{
 public void run()
 {
  try
  {
   String a;
   int len;
   String rev="";
   System.out.println("Enter string for reverse:");
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   a=br.readLine();

   len=a.length();
   for(int i=1;i<=len;i++)
   {
       rev=rev+a.charAt(len-i);
   }
   System.out.println("Reverse :"+rev);
   }
  catch(Exception e){}
 }
}

class vowel extends Thread
{
 public void run()
 {
  try
  {
   String b;
   int len;
   System.out.println("Enter string for finding VOWEL: ");
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   b=br.readLine();

   len=b.length();
   int cnt=0;
   for(int i=0;i<len;i++)
   {
     if(b.charAt(i)=='a' || b.charAt(i)=='e' || b.charAt(i)=='i' || b.charAt(i)=='o' || b.charAt(i)=='u'
     || b.charAt(i)=='A' || b.charAt(i)=='E' || b.charAt(i)=='I' || b.charAt(i)=='O' || b.charAt(i)=='U')
     cnt++;
   }
   System.out.println("No of vowels: "+cnt);
  }
  catch(Exception e){}
 }
}
class thread1
{
    public static void main(String args[])
    {
        rev r=new rev();
        r.start();
        r.setPriority(Thread.MAX_PRIORITY);
        vowel v=new vowel();
        v.setPriority(Thread.MIN_PRIORITY);
        v.start();

    }
}

OUTPUT:

In your case output starting can be anything because we can’t predict output of threads it always gives different execution.

Enter string for reverse:
Enter string for finding VOWEL:
BaronTuts
Programming
No of vowels: 3
Reverse : stuTnoraB

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.