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.'




No comments: