Search This Blog

Sunday, July 21, 2013

Coding Bat Java String 1 solutions 1 to 18



This post contains the solutions to Java String-1 section of codingbat.com. If you are new here, you can check my previous post learn-coding-computer-programming-for-beginners. Solutions to previous sections are also available check the links to java-warmup-1 , java-warmup-2.
1.       Problem Name : helloName
Solution:
Hint: The code
Return "xyz" + name;

public String helloName(String name)
{
    return("Hello "+name+"!");
}


2.       Problem Name : makeAbba 
Solution:
Hint: e.g. "Hi" and "Bye" returns "HiByeByeHi".  Return (a+b+b+a)
      
public String makeAbba(String a, String b)
{
     return (a+b+b+a);
}     


3.       Problem Name : makeTags 
Solution:
Hint: Given tag and word strings, create the HTML string with tags around the word, e.g. "<i>Yay</i>".  ‘i’ should be replaced by whatever is given in tag string and Yay should be replaced by whatever is given in word string.

public String makeTags(String tag, String word)
{
       String starttag="<"+tag+">";
       String endtag="</"+tag+">";

       return(starttag+word+endtag);
}

  
4.       Problem Name : makeOutWord 
Solution:
Hint:  Need to enclose the words in the given parenthesis and return the string.  The out word contains e.g. “<<>>” the first two characters represents our start tag and last two characters of out represents end tag. Return Start

public String makeOutWord(String out, String word)
{
       int len=out.length();// length of out string

       String starttag=out.substring(0,2); // to take << from out string
       String endtag=out.substring(len-2,len); // to take >> from out string
      
       return(starttag+word+endtag);
}


5.       Problem Name : extraEnd 
Solution:
Hint:   return a new string made of 3 copies of the last 2 chars of the original string.

public String extraEnd(String str)
{
       String end = str.substring(str.length()-2);
               
       return end + end + end;
}


6.       Problem Name : firstTwo 
Solution:
Hint:  return the string made of its first two chars, so the String "Hello" yields "He".
Constraint: If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string ""


public String firstTwo(String str)
{
        if (str.length() >= 2)
        {
            return str.substring(0, 2);
        }
        else
        {
            return str;
        }
}


7.       Problem Name : firstHalf
Solution:
Hint: Given a string of even length, return the first half. So the string "WooHoo" yields "Woo"

public String firstHalf(String str)
{
       int len=str.length();
               
       if(len%2==0)
       {
             int half=len/2;
             return(str.substring(0,half));
       }
       else
       {
              return " ";
       }
}

8.       Problem Name : withoutEnd 
Solution:
Hint:   Given a string, return a version without the first and last char, so "Hello" yields "ell".
Constraint:  The string length will be at least 2. 

public String withoutEnd(String str)
{
       return (str.substring(1,str.length()-1));
}


9.       Problem Name : comboString 
Solution:
Hint:   Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside.
Constraint:  The strings will not be the same length, but they may be empty (length 0)


public String comboString(String a, String b)
{
       if(a.length()>b.length())
               return(b+a+b);
       else
               return(a+b+a);
}


10.   Problem Name : nonStart 
Solution:
Hint:   Given 2 strings, return their concatenation, except omit the first char of each
Constraint: The strings will be at least length 1. 

public String nonStart(String a, String b)
{
       return(a.substring(1,a.length())+b.substring(1,b.length()));
}


11.   Problem Name : left2 
Solution:
Hint: Given a string, return a "rotated left 2" version where the first 2 chars are moved to the end
Constraint: The string length will be at least 2. 

public String left2(String str)
{
       return str.substring(2) + str.substring(0, 2);
              //substring(2) extracts the string from index 2 through the end. So we put that first, followed by substring(0, 2)
}

12.   Problem Name : right2
Solution:
Hint:  Given a string, return a "rotated right 2" version where the last 2 chars are moved to the start.
Constraint: The string length will be at least 2.

public String right2(String str)
{
       int len=str.length();
       if(len>2)
       {
               return(str.substring(len-2,len)+str.substring(0,len-2));
       }
       else
       {
               return str;
       }
}


13.   Problem Name : theEnd
Solution:
Hint:  Given a string, return a string length 1 from its front, unless front is false, in which case return a string length 1 from its back.
Constraint: The string will be non-empty. 

public String theEnd(String str, boolean front)
{
       if(front)
              return(str.substring(0,1));
       else
              return (str.substring(str.length()-1,str.length()));
}


14.   Problem Name : withouEnd2 
Solution:
Hint: Given a string, return a version without both the first and last char of the string. 
Constraint: The string may be any length, including 0. 

public String withouEnd2(String str)
{
       if(str.length()>2)
       {
              return(str.substring(1,str.length()-1));
       }
       else
              return "";
}


15.   Problem Name : middleTwo 
Solution:
Hint: Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri".
Constraint: The string length will be at least 2.

public String middleTwo(String str)
{
       if(str.length()>2)
       {
               int middle=str.length()/2;
               return(str.substring(middle-1,middle+1));
       }
       else
       {
               return str;
       }              
return result;
}


16.   Problem Name : endsLy 
Solution:
Hint: Given a string, return true if it ends in "ly".

public boolean endsLy(String str)
{
       int len=str.length();
       if(len>=2)     
       {
              if(str.substring(len-2,len).equals("ly"))
                return true;
              else
                return false;
       }
       else
       {
                return false;
       }
}


17.   Problem Name : nTwice 
Solution:
Hint: return a string made of the first and last n chars from the string.

public String nTwice(String str, int n)
       {
                return (str.substring(0,n)+str.substring(str.length()-n,str.length()));
       }

18.   Problem Name : twoChar
Solution:
Hint: return a string length 2 starting at the given index. If the index is too big or too small to define a string length 2, use the first 2 chars.
Constraint: The string length will be at least 2.


public String twoChar(String str, int index)
       {
                int len=str.length();
                if(index>=0 && index+2<=len)
                return(str.substring(index,index+2));
                else
                return (str.substring(0,2));
       }

Please check the my posts for solutions to the remaining problems of Java String 1 section.

No comments:

Post a Comment

Labels