CSci 2101 Data Structures: Lab 6

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",
	    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:

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 ");
	sb2 = sb1.replace(0,4,"any");
	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.