xhtml
 

Exercise 12: Equality

Here is the start of a program to compare two strings. Your task is to adapt it so that it takes the two strings as command line arguments, and prints out a message to say whether or not the strings are equal.

How do you compare two strings? If they are s1 and s2 then you can't write s1 == s2 to compare them. Think of a string as an array of characters (which is not quite right, but near enough for now). Writing s1 == s2 means that you are testing whether they are the same array (i.e. their addresses, or pointers, are the same). Instead, you want to know whether the characters inside them are the same. Fortunately, you don't have to write an explicit loop. Instead you can use a ready made function and write s1.equals(s2). For C programmers, this is the equivalent of using the C function strcmp.

Hint 1

Hint 2

Hint 3

Hint 4


Back