Showing posts with label JAVA BAT : String 1. Show all posts
Showing posts with label JAVA BAT : String 1. Show all posts

Friday, March 11, 2011

JAVA BAT : String 1 : firstTwo

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

First Two Problem: Given a string, return the string made of its first two chars, so the String "Hello" yields "He". If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string "" yields the empty string "". Note that str.length() returns the length of a string.
Examples:
firstTwo("Hello") → "He"
firstTwo("abcdefg") → "ab"
firstTwo("ab") → "ab"

Solution for this JavaBat firstTwo problem:


public String firstTwo(String str) {          

     if(str.length()<2){
         return str;
     }
     else{
         return str.substring(0,2);
     }

}

Tuesday, February 22, 2011

JAVA BAT : String 1 : extraEnd

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Extra End Problem : Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.
Examples:
extraEnd("Hello") → "lololo"
extraEnd("ab") → "ababab"
extraEnd("Hi") → "HiHiHi"

Solution for this JavaBat extraEnd problem:

public String extraEnd(String str) {

  String lastchars = str.substring(str.length()-2,str.length());
  return lastchars+lastchars+lastchars;

}


Tuesday, February 1, 2011

JAVA BAT : String 1 : Make Out Word

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Make Out Word Problem:Given an "out" string length 4, such as "<<>>", and a word, return a new string where the word is in the middle of the out string, e.g. "<>". Note: use str.substring(i, j) to extract the String starting at index i and going up to but not including index j.
Examples:
makeOutWord("<<>>", "Yay") → "<<Yay>>"
makeOutWord("<<>>", "WooHoo") → "<<WooHoo>>"
makeOutWord("[[]]", "word") → "[[word]]"



In this problem we have a method named makeOutWord with two string parameter. The first parameter is a length 4 string and the second string parameter will come in the middle of the first string or in other words left half of the first string will come before the second word and the right half of first string will come after the second word. 

Solution for this JavaBat makeOutWord problem:


    public String makeOutWord(String outside, String inside)
    {

      String left = outside.substring( 0, 2 );
      String right = outside.substring( 2, 4 );
      String str = left + inside + right;
      return str;
    }



This code can also be written in this way:


    public String makeOutWord(String outside, String inside)
    {
      String str = outside.substring( 0, 2 ) + inside 
           + outside.substring( 2, 4 );
      return str;
    }


Thursday, January 13, 2011

JAVA BAT : String 1 : Make Tags

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Make Tags Problem: The web is built with HTML strings like "Yay" which draws Yay as italic text. In this example, the "i" tag makes and which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word, e.g. "Yay".
Examples:
makeTags("i", "Yay") → "<i>Yay</i>"
makeTags("i", "Hello") → "<i>Hello</i>"
makeTags("cite", "Yay") → "<cite>Yay</cite>"

In this problem two words are passed as parameter to a method makeTags, one is 'tag' and other is 'word'. The makeTags method should return a string formatted in a way that the tag is around the word as like a HTML tag.

To solve this problem first we have to create a HTML tags from the given 'tag' string.  For this we will create a start tag and an end tag.


                    String starttag = "<"+tag+">";
                    String endtag =
"</"+tag+">";
 

Now we have to place these tags around the words.


                    String str = starttag+word+endtag;

So the Answer for this Java Bat Problem:

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

          }
 

Tuesday, January 11, 2011

JAVA BAT : String 1 : makeAbba

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(),  str.substring(i, j) etc.

Make ABBA Problem: Given two strings, a and b, return the result of putting them together in the order abba.
Examples:
makeAbba("Hi", "Bye") → "HiByeByeHi"
makeAbba("Yo", "Alice") → "YoAliceAliceYo"
makeAbba("x", "y") → "xyyx"

In this problem a method that has two string as parameter for example string A and B should return a string that is in the order ABBA. For example 'Hi' and 'Bye' are passed as parameter then the result should be 'HiByeByeHi'.

Answer For this Java Bat problem:


                   public String makeAbba(String a, String b) {
                                      String str=a+b+b+a;
                                      return
str;
                   }
 
  
Above Code can also be written in this way:


                   public String helloName(String a, String b) {

                                          
 return a+b+b+a;

                   }

   
 

JAVA BAT : String 1 : Hello Name

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i,j) etc.

Hello Name Problem: Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".

Examples :
helloName("Bob") → "Hello Bob!"
helloName("Alice") → "Hello Alice!"
helloName("X") → "Hello X!"

So, in this problem a string is passed to a method helloName and that method should return a string that have a 'Hello ' concatenated at front and '!' concatenated at the end. As Shown in Examples above. 

Answer For this Java Bat problem:


                         public String helloName(String name) {
                                           name= "Hello "+name+"!";
                                         
 return name;
                         }
 
 
Above Code can also be written in this way:


                         public String helloName(String name) {

                                         
 return "Hello "+name+"!";

                         }

   

JAVA BAT : String 1 Problems

In String 1 section of Java Bat there are basic string problems in which you cannot use loops to solve the problem. We should use string functions like str.length(), str.substring(i, j) etc.

JAVA BAT String 1 Problems:

 helloName makeAbba makeTags
 makeOutWord extraEnd firstTwo
 firstHalf withoutEnd comboString
 nonStart left2 right2
 theEnd withouEnd2 middleTwo
 endsLy nTwice twoChar
 middleThree hasBad atFirst
 lastChars conCat lastTwo
 seeColor frontAgain minCat
 extraFront without2 deFront
 startWord withoutX withoutX2