Documentation
The Java™ Tutorials
Download Ebooks
Download JDK
Search Java Tutorials
Trail: Learning the Java Language
Lesson: Numbers and Strings
Home Page > Learning the Java Language > Numbers and Strings
« Previous • TOC

# Answers to Questions and Exercises: Characters and Strings

## Questions

1. Question: What is the initial capacity of the following string builder?

       StringBuilder sb = new StringBuilder("Able was I ere I saw Elba.");

   Answer: It's the length of the initial string + 16: 26 + 16 = 42.

2. Consider the following string:

       String hannah = "Did Hannah see bees? Hannah did.";

   1. Question: What is the value displayed by the expression `hannah.length()`?

      Answer: 32.

   2. Question: What is the value returned by the method call `hannah.charAt(12)`?

      Answer: `e`.

   3. Question: Write an expression that refers to the letter `b` in the string referred to by `hannah`.

      Answer: `hannah.charAt(15)`.

3. Question: How long is the string returned by the following expression? What is the string?

       "Was it a car or a cat I saw?".substring(9, 12)

   Answer: It's 3 characters in length: `car`. It does not include the space after car.

4. Question: In the following program, called `ComputeResult`, what is the value of `result` after each numbered line executes?

       public class ComputeResult {
           public static void main(String[] args) {
               String original = "software";
               StringBuilder result = new StringBuilder("hi");
               int index = original.indexOf('a');

       /*1*/   result.setCharAt(0, original.charAt(0));
       /*2*/   result.setCharAt(1, original.charAt(original.length()-1));
       /*3*/   result.insert(1, original.charAt(4));
       /*4*/   result.append(original.substring(1,4));
       /*5*/   result.insert(3, (original.substring(index, index+2) + " "));

               System.out.println(result);
           }
       }

   Answer:

   1. si
   2. se
   3. swe
   4. sweoft
   5. swear oft

## Exercises

1. Exercise: Show two ways to concatenate the following two strings together to get the string `"Hi, mom."`:

       String hi = "Hi, ";
       String mom = "mom.";

   Answer: `hi.concat(mom)` and `hi + mom`.

2. Exercise: Write a program that computes your initials from your full name and displays them.

   Answer: `ComputeInitials`

       public class ComputeInitials {
           public static void main(String[] args) {
               String myName = "Fred F. Flintstone";
               StringBuffer myInitials = new StringBuffer();
               int length = myName.length();

               for (int i = 0; i < length; i++) {
                   if (Character.isUpperCase(myName.charAt(i))) {
                       myInitials.append(myName.charAt(i));
                   }
               }
               System.out.println("My initials are: " + myInitials);
           }
       }

3. Exercise: An anagram is a word or a phrase made by transposing the letters of another word or phrase; for example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft." Write a program that figures out whether one string is an anagram of another string. The program should ignore white space and punctuation.

   Answer: `Anagram`

       public class Anagram {

           public static boolean areAnagrams(String string1,
                                             String string2) {

               String workingCopy1 = removeJunk(string1);
               String workingCopy2 = removeJunk(string2);

       	    workingCopy1 = workingCopy1.toLowerCase();
       	    workingCopy2 = workingCopy2.toLowerCase();

       	    workingCopy1 = sort(workingCopy1);
       	    workingCopy2 = sort(workingCopy2);

               return workingCopy1.equals(workingCopy2);
           }

           protected static String removeJunk(String string) {
               int i, len = string.length();
               StringBuilder dest = new StringBuilder(len);
         		char c;

       	    for (i = (len - 1); i >= 0; i--) {
       	        c = string.charAt(i);
       	        if (Character.isLetter(c)) {
       		        dest.append(c);
       	        }
       	    }

               return dest.toString();
           }

           protected static String sort(String string) {
       	    char[] charArray = string.toCharArray();

       	    java.util.Arrays.sort(charArray);

               return new String(charArray);
           }

           public static void main(String[] args) {
               String string1 = "Cosmo and Laine:";
               String string2 = "Maid, clean soon!";

               System.out.println();
               System.out.println("Testing whether the following "
                                + "strings are anagrams:");
               System.out.println("    String 1: " + string1);
               System.out.println("    String 2: " + string2);
               System.out.println();

               if (areAnagrams(string1, string2)) {
                   System.out.println("They ARE anagrams!");
               } else {
                   System.out.println("They are NOT anagrams!");
               }
               System.out.println();
           }
       }

« Previous • TOC

Previous page: Questions and Exercises: Characters and Strings