Go to Top

Friday 23 December 2011

To manipulate vowels in a string

Question 72 : Write a program in Java to accept a word and do the following : 

1) If the word starts with a vowel, then move the vowel to the end of the word and add "ay" to it. 
Example : owl -> wloay.
2) If there are no vowels in the word, then just add "ay" to it.
Example : sky -> skyay.
3) If the vowel lies in between, then remove all the letters from the beginning till the vowel and add them at the end of the word. Finally, attach "ay" to it.
Example : stop -> opstay.

Java Program :

import java.io.*;
class vowel_changer
{
    static String s,s1;
   static void main()throws IOException
   {
       input();
       manipulate();
       display();
    }
  static void input()throws IOException
  {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter a word : ");
      s=br.readLine();
    }
    static void manipulate()
    {
        int i,c=0;
        char k;
        for(i=0;i<s.length();i++)
        {
            k=s.charAt(i);
            if(k=='a' || k=='A' || k=='e' || k=='E' || k=='i' || k=='I' || k=='o' || k=='O' || k=='u' || k=='U')
            {
                if(i==0)
                s1=s.substring(1,s.length())+k+"ay";
                else
                s1=s.substring(i,s.length())+s.substring(0,i)+"ay";
                c=1;
                break;
            }
        }
        if(c==0)
        s1=s+"ay";
    }
    static void display()
    {
        System.out.print("\nManipulated String : "+s1);
    }
}
                 

1 comment:

  1. I saw this interesting question in a kind of question paper, decided to solve it and now you have it here all done.

    ReplyDelete

ShareThis