Thursday, May 1, 2014

String Function - I [ Palindrome & Longest Word ]




When reverse value of a word, sentence or a number is same as its original input is known as palindrome.


import java.io.*;
    public class StringPalindrome
        {
            public static void main(String args[])throws IOException
                {
                    String Str;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter a String : ");
                    Str=Br.readLine();
                    StringPalindrome S=new StringPalindrome();
                    if(Str.equalsIgnoreCase(S.isPalin(Str)))
                        {
                            System.out.print("\n\t"+Str+" is a Palindrome Word");
                        }
                    else
                        {
                            System.out.print("\n\t"+Str+" is not a Palindrome Word");   
                        }

               }
            String isPalin(String S)
                {
                    String Str1="";
                    for(int i=0;i<s.length();i++)
                        {
                            Str1=S.charAt(i)+Str1;
                        }
                    return Str1;
                }
        }


Output :
          Enter a String : Madam
          Madam is a Palindrome Word


Program to find the largest word in a Sentence


import java.io.*;
    public class LargestWord
        {
            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();
                    LargestWord S=new LargestWord();
                    S.Large(Str);
                }  
            void Large(String S)
                {
                    String Str1="";
                    S=S+" ";
                    int j=0,Len=0;
                    for(int i=0;i<s.length();i++)
                        {
                            if(S.charAt(i)==' ')
                                {
                                    String Str2=S.substring(j,i);
                                    j=i+1;
                                    Str2=Str2.trim();
                                    if(Str2.length()>Len)
                                        {
                                            Len=Str2.length();
                                            Str1=Str2;
                                        }
                                }   
                        }
                    System.out.println("\n\tLargest Word in '"+S+"' is '"+Str1+"' the Length is = "+Len);
                }
        }


Output :
Enter a Sentence : To find the largest word in a String

          Largest Word in 'To find the largest word in a String ' is 'largest' the Length is = 7



No comments: