

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Here is the Java API webpage for class String. Each object contains a list, or string, of characters. Any string lit- eral in your, like “abc” is maintained in a object of class String. This description tells us two important things. First, String objects are immutable; they cannot be changed. Second, a String is implemented as an array of values of type char. You don’t yet know about arrays in Java, but they are much like arrays in other programming languages. So, here’s how we might represent the array containing the String “bcdefgh”. Knowing that a String is implemented in an array can help you estimate how fast various op- erations are. For example, to find the index of a character may take time proportional to the length of the string since each character may have to be looked at to find c in the array. When talking about a string s, we often use the notation s[k] to denote the character at index k. This notation is useful, even if it is not Java. In the same way, s[h..k] denotes the substring of s that contains the characters s[h], s[h+1], …, s[k]; s[h..] denotes the substring of s starting at h and going to the end; and s[h..h-1] denotes the empty string starting at position h. At this point, take 5 minutes to look through the Method Summaries and Details of the following String meth- ods, because you will be using them in writing methods that manipulate strings. Some of them are overloaded. Instance methods length charAt substring indexOf indexOf lastIndexOf lastIndexOf startsWith startsWith contains compareTo equals trim replace toLowerCase() toUpperCase() Static methods: valueOf There are two important points to remember.
Otherwise, we have to construct a string consisting of the last character of the first name, which is just before the blank character s[k], catenated with everything that comes before, s[0..k-2], but all in upper case. Now let’s translate this into java. First comes s[k-1] —we use function charAt. Next comes the substring s[0..k-2] —we use function substring, but note that the second parameter is k–1 and not k–2 because of the way substring is defined. Now, we have to make all those characters into upper case —we use instance function toUpperCase. We are done. Note that the code works if the first name is a single character. That’s because s[0..-1] and s.substring(0, 0) are the empty string “”. /**String s contains a list of at least one name. Adjacent names separated by one or more space characters. If there is only one name, return it; otherwise, return the first name but with its last character placed at the beginning and all chars in upper case. */ public static char getName(String s) // Remove spaces at beginning and end s= trim(); // Store in k the index of the first blank in s (-1 if none) int k= s.indexOf(‘ ‘); if (k == - 1) return s; // return s[k–1] + s[0..k–2], all in upper case return (s.charAt(k–1) + s.substring(0, k–1)).toUpperCase(); }