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?



No comments: