## CSci 2101 Data Structures: Lab 7

### Problem 1: working with strings

For this problem you will work with an array of Strings. Below is the file that you will write your code in. You may, of course, change the elements in the array to test the program. Comment out (do not delete) your test cases.

Important: Make sure that your program doesn't require any changes if the number of elements in the array changes.

Question 1 Write a loop to count the number of case-sensitive and non-case-sensitive matches for a given string in the array. Print out the counters after the loop. Test your code on the given array and the strings `find1` and `find2` (see the program below).

Question 2 Write a loop to find the longest string in the array. Print out the longest string after the loop. If several strings have the same length, any one of them will do.

Question 3 Define a new string `glueAll`. Write a loop which goes through the array and concatenates all the strings of the array, separated by spaces, into the string `glueAll`. After the loop replace the last white space in `glueAll` by `!`.
For example, if the array is `{"Roses","are","red"}`, the string `glueAll` will have
`Roses are red!`
in it.

``````
public class StringArray {

public static void main(String[] args) {
String [] strings = {"I","do","not","like","green","eggs","and",
"ham","I","DO","Not","like","it","Sam","I",
"am"};
String find1 = "I";
String find2 = "not";

// Quest. 1: find all case-sensitive and non-case-sensitive
// matches for each of the given strings

// Quest. 2: find the longest string in the array, print it

// Quest. 3: concatenate all the strings into one string

}

}
``````

### Problem 2: strings and characters

Write a program to do the following:
• Create a string with some characters in it (they may be letters, digits, punctuation marks, etc.)
• Write a loop that goes through the string character-by-character using a charAt() method of a String (see String API). In the loop count the number of letters in the string. To check if a character is a letter, use the method isLetter() of the class Character. More precisely,
``````
char c = 'A';
boolean b1 = Character.isLetter(c);
c = '6';
boolean b2 = Character.isLetter(c);
``````
makes b1 true because 'A' is a letter and makes b2 false because '6' is not a letter.
• Print the value of the letter counter after the loop.
• Test your program on several different strings, comment out your test cases and write the results.

### Problem 3: memory picture

Draw the memory picture for the program below. What will the program print?
Run the program to test your results. Revise your picture if needed.
``````
public class StringsAndBuffers {
public static void main(String [] args) {
String s1 = "something";
String s2 = s1.toUpperCase();
System.out.println("s1.equals(s2) = " + s1.equals(s2));

String s3 = s2.toLowerCase();
s2 = s3;
System.out.println("s1 == s2 is " + (s1 == s2));
System.out.println("s2 == s3 is " + (s2 == s3));

StringBuffer sb1 = new StringBuffer("something else");
StringBuffer sb2 = new StringBuffer("something different");
StringBuffer sb3 = sb2;
sb3.insert(10,"entirely ");
System.out.println(sb2);
sb2 = sb1.replace(0,4,"any");
System.out.println(sb1);
System.out.println(sb2);
System.out.println("sb1 == sb2 is " + (sb1 == sb2));

}
}
``````

### Problem 4: Palindromes

Palindromes are words that read the same forward and backward. In the program below write a loop to print out all palindromes in the array of StringBuffers. Consider the palindromes to be case-insensitive, so "Bob" is a palindrome. Use the method reverse() of the class StringBuffer. Unfortunately, StringBuffer has neither equals() nor equalsIgnoreCase() methods, so you have to convert it to a String to compare. If sb is a StringBuffer, sb.toString() returns a string with the same contents as in the string buffer.

Important: do not change the given array. Print it out at the end to make sure that no elements have changed.

``````
public class Palindromes {
public static void main(String [] args) {
StringBuffer [] words = new StringBuffer[7];
words[0] = new StringBuffer("Rob");
words[1] = new StringBuffer("sees");
words[2] = new StringBuffer("Bob");
words[3] = new StringBuffer("and");
words[4] = new StringBuffer("Bob's");
words[5] = new StringBuffer("sister");
words[6] = new StringBuffer("Anna");

// write a loop to print out all the palindromes

}
}
``````

### Problem 5 (extra credit)

Start on this problem only after you have finished, checked, and submitted the rest of the lab.
Write a program to count occurrences of the string `red` in a given string (case-sensitive). For instance, the following meaningless sentence contains 4 occurrences of `red`:
`Alfred gets credit for this incredible reduction`
Use indexOf() method of String.
This is a lab from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.