Wednesday, 15 July 2015

Java Programs Made Easy

Q)...Two input strings are accepted. If the two strings are of same length concat them and return, if they are not of same length, reduce the longer string to size of smaller one, and concat them
input1:"hello"
input2:"hi" 
output:"lohi"
input1:"aaa"
input2:"bbb"
output:"aaabbb"



  1. package Set1;
  2. import java.util.*;
  3. public class ClassSet18 {
  4.  public static String concatStrings(String s1,String s2){
  5.   StringBuffer sb=new StringBuffer();
  6.   if(s1.length()==s2.length())
  7.    sb.append(s1).append(s2);
  8.   else if(s1.length()>s2.length()){
  9.    s1=s1.substring(s1.length()/2, s1.length());
  10.    sb.append(s1).append(s2);
  11.   }
  12.   else if(s1.length()<s2.length()){
  13.    s2=s2.substring(s2.length()/2, s2.length());
  14.    sb.append(s1).append(s2);
  15.   }
  16.   return sb.toString();
  17.  }
  18.  public static void main(String[] args) {
  19.   Scanner s=new Scanner(System.in);
  20.   String s1=s.next();
  21.   String s2=s.next();
  22.   System.out.println(concatStrings(s1,s2));
  23.  }
  24. }