I've been given this question of string comparison. I had to write a method to compare two strings without using java's built in string comparison methods. Also it suppose to be around 3 - 5 lines of code long. The method should return 0 for equal, 1 for string 'a' is bigger then string 'b', -1 for string 'a' is smaller then 'b'
Now, I know Java compares string based on the int value of each char so I tried to do this thing, which works but is definitely not 3-5 lines of code long:
public int compare(String s1, String s2){
int result = 0;
int count = 0; // The counter for the first string integer values sum
int count2 = 0; // The counter for the second string integer values sum
for(int c=0; c<s1.length(); c++){
count = count +s1.charAt(c);
}
for (int c2=0; c2<s2.length(); c2++){
count2 = count2 +s2.charAt(c2);
}
//***** some condition statement to check which is bigger and then return the result