Write a recursive method compareStringsString s1 String s2 T
Write a recursive method compareStrings(String s1, String s2). This method should return 0 if the strings s1 and s2 contain exactly the same characters in the same order, 1 if s1 is alphabetically (using ASCII values) \"greater than\" s2, or -1 if s1 is alphabetically \"less than\" s2. Examples: compareStrings(\"Enterprise\", \"defiant\") should return -1 (since ASCII values for capitals precede those for lower-case characters) compareStrings(\"enterprise\", \"enter\") should return 1 compareStrings(\"Excelsior\", \") should return 1 compareStrings(\"\".\"\"Excelsior\") should return -1 In the RecursiveLL class that we discussed in lecture, add recursive methods for each of the following wrappers. Your recursive methods should add at least one parameter to keep track of the current node in the list. You can use the posted code as a starting point. contains(E someltem) - returns true if someltem exists in the list, false if not. search(E someltem) - returns the index of the first occurrence of someltem if it exists in the list, or - 1 if someltem is not in the list. reverse() - reverses the calling list.
Solution
Hi, Please find implementation for Q1.
Please post other question in separate post.
Please let me know in case of any issue.
// compare string implementation
int compareStrings(String s1, String s2){
// if they have diffrent length, they can not be eqaul
if(s1.length() > s2.length())
return 1; // s1 > s2
if(s1.length() < s2.length())
return -1;
for(int i=0; i<s1.length(); i++){
if(s1.charAt(i) < s2.charAt(i))
return -1;
else if(s1.charAt(i) > s2.charAt(i))
return 1;
}
return 0; // both are equal
}
