Thursday, May 8, 2014

String Function - II [ Piglatin & Find ]


Pig Latin is believed to be secret language which is used in English.

When a word begins with consonant, move all the consonant at the end until the first Vowel and at the end add ‘ay’, finally how it is looked like -

Eg. King  - ingKay

However when word start with a vowel, only ‘ay’ is added at the end.

Eg. Egg    -  Eggay


import java.io.*;
    public class Piglatin
        {
            public static void main(String args[])throws IOException
                {
                    String Str;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter a Sentence : ");
                    Str=Br.readLine();
                    Piglatin S=new Piglatin();
                    S.Word(Str);
                }
            void Word(String S)
                {
                    char ch=' ';
                    String Rstr="";
                    if(!Character.isLetter(S.charAt(S.length()-1)))
                        {
                            ch=S.charAt(S.length()-1);
                            S=S.substring(0,S.length()-1);
                        }
                    S=S+" ";
                    for(int i=0;i<S.length();i++)
                        {
                            String TmpS="";
                            while(S.charAt(i)!=' ')
                                {
                                    TmpS=TmpS+S.charAt(i++);
                                }
                            TmpS=PigL(TmpS);
                            Rstr+=TmpS+" ";
                        }
                    Rstr=Rstr.trim();   
                    Rstr=Character.toUpperCase(Rstr.charAt(0))+Rstr.substring(1)+ch;
                  
                    System.out.print("\t"+Rstr);   
                }
            String PigL(String S)
                {
                    String Vowel="AEIOUaeiou";
                    String Pstr="";
                    int i=0,j=0;
                    outer:
                    for(i=0;i<S.length();i++)
                        {
                            for(j=0;j<Vowel.length();j++)
                                {
                                    if(S.charAt(i)==Vowel.charAt(j))
                                        {
                                            break outer;
                                        }
                                }
                         }
                    if(j>0 && j&gt10)
                         {
                              Pstr=S.substring(i)+S.substring(0,i)+"ay";
                         }
                    else
                        {
                            Pstr+="ay";
                        }
                    return Pstr;
                 }
        }


Output:

          Enter a Sentence : What is yout name?
          AtWhay isay outyay amenay?

          Enter a Sentence : egg
          Eggay



Find a word within a sentence and also count numbers of times it presents.


import java.io.*;
    public class Find
        {
            public static void main(String args[])throws IOException
                {
                    String Str,Sword;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter a Sentence : ");
                    Str=Br.readLine();
                    System.out.print("\n\tEnter a Word to Search : ");
                    Sword=Br.readLine();                   
                    Find S=new Find();
                    int cnt=S.FindWord(Str,Sword);
                    System.out.println("\tSentence : '"+Str+"'\n\tSearch Word : '"+Sword+"'");
                    System.out.println("\tFound : "+cnt+" Times");
                }
            int FindWord(String S,String S1)
                {
                    int j=0,Wcnt=0;
                    S=S.toUpperCase();
                    S1=S1.toUpperCase();
                    for(int i=0;i<S.length();i++)
                        {
                            int k=S.indexOf(S1,j);
                            if(k>=0)
                                {
                                    Wcnt++;
                                    j=k+S1.length();
                                    i=j;
                                }
                        }
                    return Wcnt;   
                }
        }


Output:



          Enter a Sentence : Happiness is not something you postpone for the future; it is something you design for the present.

          Enter a Word to Search : is
          Sentence : 'Happiness is not something you postpone for the future; it is something you design for the present.'

          Search Word : 'is'

          Found : 2 Times




No comments: