Thursday, November 18, 2010

program for display next vowel if vowel and display next consonent if consonent in java

import java.util.*;
class letter
    {
        public void wordsort(String st)
            {
                StringTokenizer stk=new StringTokenizer(st);
                int i=stk.countTokens();
               
                for(int j=1;j<=i;j++)
                    {
                        String wrd=stk.nextToken();
                    }
                   String res=sort(st);
                       String rest=encode(res)+" ";
                        System.out.println(rest);
                   
            }
         public String sort(String wrd)
            {  
               int g=0,i,j,d=0;
               String p="",res="";wrd+=" ";
               int l=wrd.length();
               for (j=0;j<l;j++)
                {
                    if(wrd.charAt(j)==' ')
                    d++;
                }
                String arr[]=new String[d];
                for(i=0;i<=l-1;i++)
                    { 
                       
                        if(wrd.charAt(i)!=' ')
                        p=p+wrd.charAt(i);
                       
                        else 
                      {
                          arr[g]=p;
                      p="";
                        g++;
                    }
                }                           
            
               
                for(i=0;i<arr.length;i++)
                    {  int  pos=i;
                        for(j=(i+1);j<arr.length;j++)
                            {
                               if ( arr[j].compareTo(arr[pos]) <0)
                                    pos=j;
                                }      
                                       String temp =arr[pos];
                                       arr[pos] = arr[i] ;
                                        arr[i]=temp;
                                    }
                                   
                  
                   
                 for(i=0;i<arr.length;i++)
                    res+=arr[i]+" ";  
                return(res);
            }
         public String encode(String st)
            {
                String cvow="AEIOU",svow="aeiou",ccon="BCDFGHJKLMNPQRSTVWXY",scon="bcdfghjklmnpqrstvwxy";
                int i,j,k,l;
                char ch;
                String res="";
                for(i=0;i<st.length();i++)
                    {   
                        ch=st.charAt(i);
                        if(ch == 'U')
                            res+='A';
                        else if(ch == 'u')
                            res+='a';
                        else if(ch == 'Z')
                            res+='B';
                        else if(ch == 'z')
                            res+='b';
                        else if(ch==' ')
                        res+=' ';
                        else
                            {
                                k=0;
                                for(j=0;j<cvow.length();j++)
                                    {
                                        if(ch == cvow.charAt(j))
                                            {
                                                res+=cvow.charAt(j+1);
                                                k=1;
                                                break;
                                            }
                                    }
                                if(k == 0)
                                {
                                for(j=0;j<svow.length();j++)
                                    {
                                        if(ch == svow.charAt(j))
                                            {
                                                res+=svow.charAt(j+1);
                                                k=1;
                                                break;
                                            }
                                    }
                                }
                                if(k == 0)
                                {
                                for(j=0;j<scon.length();j++)
                                    {
                                        if(ch == scon.charAt(j))
                                            {
                                                res+=scon.charAt(j+1);
                                                break;
                                            }
                                    }
                                }
                                if(k == 0)
                                {
                                for(j=0;j<ccon.length();j++)
                                    {
                                        if(ch == ccon.charAt(j))
                                            {
                                                res+=ccon.charAt(j+1);
                                                break;
                                            }
                                    }
                                }
                             
                            }                           
                           
                    }
                  return(res);
            }
    }

No comments: