Preview

24 - Algorithms Using Strings

 1. The following example shows:
public class StringContainsExample {
 
    public static void main(String[] args) {
        
        String str = "Hello" + "World";
        System.out.println(str);
        
    }
}

  string addition using ASCII (Java enabled)

  An erroneous (incorrect) command that will fail to compile as two strings cannot be added together

  string literals being deleted

  string concatenation

 2. What is the following code likely to be doing?
import java.util.Arrays;
 
public class StringSplitByWords {
    public static void main(String[] args) {
        String sentence = "Java String split by words from sentence";
        String[] words = splitSentenceByWords(sentence);
        System.out.println(Arrays.toString(words));
    }
    
    private static String[] splitSentenceByWords(String str){        
      array
        if(str == null || str.equals(""))
            return new String[0];
        String[] words = str.split(" ");
        return words;        
    }
}

  The sentence is broken down into words by splitting it using space.

  The sentence is broken up into two phrases using the str=null command

  The sentence is reversed and printed in reverse order

  The sentence is split into individual letters using space

 3. Fill in the blanks for the last line of this Java code snippet
private static String capitalizeEachWord(String str){
    
    //if string is null or empty, return empty string
    if(str == null || str.length() == 0)
        return "";
    
    /* 
     * if string contains only one char,
     * make it capital and return 
     */
    if(str.length() == 1)
        return str.toUpperCase();
    
    
    /*
     * Split the string by word boundaries
     */
    String[] words = str.split("\\W");
    
    //create empty StringBuilder with same length as string
    StringBuilder sbCapitalizedWords = new StringBuilder(str.length());
    
    for(String word : words){            
        
        if(word.length() > 1)
            sbCapitalizedWords
                .append(word.substring(0, 1).toUpperCase())
                .append(word.substring(1));
        else
            sbCapitalizedWords.append(word.toUpperCase());
        
        //we do not want to go beyond string length
        if(sbCapitalizedWords.length() < str.length()){
            sbCapitalizedWords.append(str.charAt(sbCapitalizedWords.length()));
        }
    }
    
    /*
     * convert StringBuilder to string, also
     * remove last space from it using trim method, if there is any
     */
    ????_____________________________________????
}

  return sbCapitalizedWords.toString();

  return sbCapitalizedWords.trim();

  return sbCapitalizedWords.toString().trim();

  return sbWords.toString();

 4. Here convertion of CharSequence to a String takes place and then indexOf method is called. The method indexOf returns O or a higher number if it finds the String, otherwise NULL is returned.
public boolean contains(CharSequence sequence)
{
   return indexOf(sequence.toString()) > -1;
}

  TRUE

  FALSE

 5. The output of the following code is "W".
class TTIO { 
    public static void main(String args[]) 
    { 
        String s = "Welcome to testandtrack.io"; 
  
        char ch = s.charAt(3); 
        System.out.println(ch); 
  
        ch = s.charAt(0); 
        System.out.println(ch); 
    } 
} 

  FALSE

  TRUE