View Single Post
  #1 (permalink)  
Old 03-08-2008, 08:50 PM
amexudo amexudo is offline
Member
 
Join Date: Mar 2008
Posts: 2
amexudo is on a distinguished road
exercise of recursive method
hello,
i was trying to do an exercise but i cannot resolve it.
the exercise asks us to write a recursive method
public static int myCompare (String s1, String s2)
that receives 2 strings and compare them.
it returns 1 if string s1 comes after s2 in the dictionary (lexicographly)
the method returns 2 if string s1 comes after s2 in the dictionary
and it returns 0 if strings s1 and s2 are exactly the same.
example:
input:
s1 = "mother" and s2 = "class"
output: 1 ('m' comes after 'c')

the method must be a recursive method and mustn't use loops.

in the solution it's only allowed to use the following existing methods (from String class):
public char charAt(int i)
public String substring(int i)
public int length()

thanks for your help!!
Reply With Quote
Sponsored Links