Thursday, May 29, 2014

String Function - V [ TCS - Assignments ]




Input a Sentence and a stretch factor, each word will be repeated according to the given stretch factor.

Ex.
String “Hello! Friends.
Stretch =2

Final String – Hello! Hello! Friends. Friends.




import java.io.*;
import java.util.*;
public class Stretch_Factor
{  
    public static void main(String[] args)throws IOException
        {
           BufferedReader Br = new BufferedReader (new InputStreamReader (System.in));
           int N=0;
           String S,S1="";;
           int P;
           System.out.print("Enter a String ");
           S=Br.readLine();
           System.out.print("Enter a Streach ");
           P=Integer.parseInt(Br.readLine());
           String List[]=S.split(" ");
           Stretch(List,P);
        }
      public static void Stretch(String List[], int P)
        {
                                      for(int j=0;j<List.length;j++)
                                                {
                                                          String s="";
                                                          for(int i=0;i<P;i++)

                                                                   {

                                                                             s=s+List[j]+" ";
                                                                   }
                                                          List[j]=s;
                                                }
                                                for(int i=0;i<List.length;i++)
                                                   {
                                                                   System.out.print(List[i]);
                                                   }

                   }           
        }

           



Output:


Enter a String Man is the artificer of his own happiness.

Enter a Streach 2

Man Man is is the the artificer artificer of of his his own own happiness. happiness.



Input a Sentence, if the word contain a number then repeat next letter for given number of time.

Ex.

String “Wo3rk”
Final String “Worrrk”



import java.io.*;
import java.util.*;
public class Repeat
{  
    public static void main(String[] args)throws IOException
        {
            BufferedReader Br = new BufferedReader (new InputStreamReader (System.in));
            int N;
            String S,S1="";;
            int P;
            System.out.print("Enter a String ");
            S=Br.readLine();
            for(int i=0;i<S.length();i++)
                {
                    if(S.charAt(i)>48 && S.charAt(i)<=57)
                        {
                            N=S.charAt(i)-48;
                            for(int j=1;j<N;j++)
                                {
                                    S1=S1+S.charAt(i+1);
                                }
                               
                               
                        }
                        else
                        S1=S1+S.charAt(i);
                   
                }
           
            System.out.println(S1);
        }
    }
           


Output:

Enter a String th2is is t3est.

thiis is teeest.



Thursday, May 22, 2014

String Function - IV [ Palindrome Words & String Sorting ]




Print the Palindrome word from a Sentence



import java.io.*;
    public class Palindrome_Word
        {
            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();
                    Palindrome_Word PW=new Palindrome_Word();
                    System.out.println("\tPalindrome Word in '"+Str+"' is : ");
                    PW.Palin(Str);
                }
            void Palin(String S)
                {
                    int j=0;
                    S=S+" ";
                    String S1,S2;
                    for(int i=0;i<S.length();i++)
                        {
                            if(S.charAt(i)==' ')
                                {
                                    S1=S.substring(j,i);
                                    S1=S1.trim();
                                    S2="";
                                    j=i+1;
                                    for(int k=0;k
                                        {
                                            S2=S1.charAt(k)+S2;
                                        }
                                    if(S1.toUpperCase().equals(S2.toUpperCase()))
                                        {
                                            System.out.println("\t"+S1);
                                        }
                                }
                        }
                }
        }



Output:



          Enter a Sentence : Mom and Dad
          Palindrome Word in 'Mom and Dad' is :
          Mom
          Dad



Sorting the Words of a Sentence.


import java.io.*;
    public class String_Sorting
        {
            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();
                    String_Sorting SS=new String_Sorting();
                    System.out.println("\tString '"+Str+"' After Sorting: ");
                    SS.Sort(Str);
                }
            void Sort(String S)
                {
                    String List[]=S.split(" ");
                    for(int i=0;i<S.length();i++)
                        {
                            for(int j=0;i<S.length()-i-1;j++)
                                {
                                    if(List[j].toUpperCase().compareTo(List[j+1].toUpperCase())>0)
                                        {
                                            String Tmp=List[j];
                                            List[j]=List[j+1];
                                            List[j+1]=Tmp;
                                        }
                                }
                        }
                    System.out.print("\t");   
                    for(int i=0;i<S.length();i++)
                        {
                            System.out.print(List[i]+" ");
                        }
                }

            }        
         





Output:




          Enter a Sentence : Hello! How are you?
          String 'Hello! How are you?' After Sorting:
          are Hello! How you?



Thursday, May 15, 2014

String Function - III [ Find-Replace & Initial ]




Print the Initial of given name



import java.io.*;
    public class Initial
        {
            public static void main(String args[])throws IOException
                {
                    String Name;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter Name : ");
                    Name=Br.readLine();
                    Initial S=new Initial();
                    S.PrintIntial(Name);
                }
            void PrintIntial(String S)
                {
                    String Result="";
                    int j=S.lastIndexOf(" ");
                    S=" "+S;
                    for(int i=0;i<S.length();i++)
                        {
                            if(S.charAt(i)==' ' && S.charAt(i+1)!=' ')
                                {
                                    Result=Result+S.charAt(i+1)+". ";
                                }
                        }
                    Result=Result+S.substring(j+1);   
                    System.out.print("\t"+Result);   
                }
        }


Output:

         Enter Name : Sachin Ramesh Tendulkar

         S. R.  Tendulkar










Find a given word in a string and replace with another



import java.io.*;
    public class Find_Rep
        {
            public static void main(String args[])throws IOException
                {
                    String Str,Sword,Rword,Result;
                    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 Replace : ");
                    Sword=Br.readLine();  
                    System.out.print("\n\tEnter a Word to Search : ");
                    Rword=Br.readLine();
                    Find_Rep S=new Find_Rep();
                    Result=S.FindRep(Str,Sword,Rword);
                    System.out.println("\tOld Sentence : '"+Str+"'");
                    System.out.println("\n\tAfter Repllaceing : '"+Sword+"'  With '"+Rword+"'");
                    System.out.println("\n\tThe New Sentence : ‘  "+Result+"'");
                }
            String FindRep(String S,String S1,String S2)
                {
                    int j=0;
                    for(int i=0;i<S.length();i++)
                        {
                            int k=S.toUpperCase().indexOf(S1.toUpperCase());
                            if(k>=0)
                                {
                                    S=S.substring(0,k)+S2+S.substring(k+S1.length());
                                    j=k+S1.length();
                                    i=j;
                                }
                        }
                    return S;   
                }
        }


Output:


          Enter a Sentence : I live in Australia. I love Australia.

          Enter a Word to Search : Australia

          Enter a Word to Replace : India
          Old Sentence : 'I live in Australia. I love Australia.'

          After Repllaceing : 'Australia'  With 'India'

          The New Sentence : I live in India. I love India.'




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