/* CSC 15 Lab 9: Strings Due in One Week As a precursor to studying arrays, we will take a closer look at the String datatype in Java. So far, you've learned that you can write code such as the following: String A; A = "abc"; A = "abc" + "def"; A = A + 'g'; // also ok to attach a single char to string if (A.equals("abcdefg")) System.out.print("A is " + A); In addition, you can pass Strings as parameters and return Strings from functions. In fact, there are a lot of other things you can do with strings. A String, is first of all, a special class in Java. Strings are objects of this class. They are special in that Strings uses certain special syntax that's not available for other objects. That is, two lines below are equivalent: A = "abc"; A = new String("abc"); The second line should look more familiar to you. This is how you create objects in general. Strings are special in that you can use the first first line to mean the same thing. Here are some other operations available on String (assumming A is a String): A.length() -> returns the number of characters that make up the string for example, if A is "abc" then A.length() returns 3. A.equals(B) -> test for string equality between A and B - you already know this one. A.charAt(i) -> returns the ith character in the string. The characters are numbered starting from 0. For example, if A is "abc", then A.charAt(0) will be 'a', A.charAt(1) is 'b' and A.charAt(2) is c. Be careful: note that A.charAt(A.length()) will never be correct because A.length() is the number of characters, but the maximum INDEX you can use with the charAt function is A.length()-1, since we start numbering from 0. Characters in Java use the "unicode" encoding, which encompasses the old "ASCII" standard. In other words (as explained in class) every character has an equivalent numerical value called its ascii code. To convert a character to its ascii code we do type casting: (int)'C' is equivalent to 67, the ascii code for upper case C. Similarly, (char)67 is equivalent to 'C'. Uppercase alphabetical characters have ascii values from 65 to 90, and lower case ones from 97 to 122. The numerical digit characters '0' through '9' have ascii values 48 ((int)'0') to 57 ((int)'9'). For this lab you are to write a number of functions that use characters and Strings. Many of these functions will require the use of while loops (or for loops). Follow the basic structure below: */ class stringfuns { // 1. Write a function public boolean isdigit(char c1) // that returns true iff c1 is a digit character ('0' through '9'), which // have ascii values from 48 to 57. // 1.5. Write a function public boolean isint(String A) // that checks if every character in the String A is a digit. For example, // isint("1205") should return true and isint("ab01") should return // false. // 2. Write a function that converts lower case characters to upper // case characters. If the parameter is not lower case, it should // return the same character. Hint: if you add 32 to 66, which is // the ascii value of 'B', you get 98, which is the ascii value of 'b'. public char forceupper(char c1) // 3. Extend the above function to Strings: public String forceupper(String S) // For example, forceupper("aBc01") should return "ABC01". // 4. Now write a function "ciequal" that determines if two strings // are equal regardless of case. You have to make up the header // but ciequal("aBc","Abc") should return true. You also need to // make sure that the two Strings are of the same length before // returning true. // 5. Write a function public String interleave(String A, String B) // that interleaves the characters in the two strings. For example, // interleave("abc","xyz") should return "axbycz". If the strings are // of different lengths, you can just return the empty string "". // 6. Note that the ascii values are ordered in the same way the // alphabet is ordered. That is, (int)'A' is less than (int)'B'. // A String is "sorted" if the characters are ordered, such as in // "ace". A string such as "cat" is not sorted since 'a' comes // after 'c'. Write a function that determines (return boolean) if // a String is sorted or not. Hint: in your loop if need to check // if (int)A.charAt(i) is less than (int)A.charAt(i+1). Be careful as // to when your loop stops should stop (it should stop before it gets // i to the last character!) // 7. A string is called a "palindrome" if it reads the same way forwards // and backwards. For example, "bob" and "noon" are both palindromes. // write a function (that returns boolean) which determines if a String // is a palindrome. You can do this problem inefficiently by first // reversing the string, and efficiently by using a single loop // that checks characters at opposite ends of the string simultaneously. // 8. Write a function that should call each of the above functions // and demonstrate that they work correctly (you usually do this in // main, but it's easier since you won't need to create an instance of // the class if you write it in the same class as the other functions. public void test() { String A, B, C; A = "012"; System.out.println("A is an int?" + isint(A)); // make up other code to test all the functions. // ... } } //stringfuns class // You shouldn't have to touch what's below: public class lab10 { public static void main(String[] args) { stringfuns SF; // need instance of class before calling functions SF = new stringfuns(); SF.test(); } // main } // lab10