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
}
}
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.
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));
}
}
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
}
}
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