java - Comparing 2 strings by ascii values ava -
java - Comparing 2 strings by ascii values ava -
i have write method compare strings alphabetically , homecoming int
. can't utilize built-in functions , i'm supposed utilize for
loop.
i'm unsure how deal strings beingness of different lengths. @ moment main issue code comparing first char of each string returning int, can't set return comparison;
outside of loop
public class poop { public static int compare(string s1, string s2) { (int = 0; < s1.length() && < s2.length(); i++) { int comparing = 0; int ascii1 = 0; int ascii2 = 0; //convert chars ascii values ascii1 = (int) s1.charat(i); ascii2 = (int) s2.charat(i); //treat capital letters lower case if (ascii1 <= 95) { ascii1 += 32; } if (ascii2 <= 95) { ascii1 += 32; } if (ascii1 > ascii2) { comparing = 1; } else if (ascii1 < ascii2) { comparing = -1; } else { comparing = 0; } } homecoming comparison; } public static void main(string[] args) { string s1 = "aba"; string s2 = "aaa"; system.out.println(compare(s1,s2)); } }
if comparing alphabetically, means first different character in string defines difference. example: aab
goes before aac
because of c
.
as different length strings, larger strings go after smaller strings (a dictionary uses convention). aaaa
goes after aaa
, because aaaa
larger.
so, let's done:
/** * homecoming integer bigger 1 if s1 "bigger" s2 */ public static int comparestrings(string s1, string s2) { int comparing = 0; int c1, c2; for(int = 0; < s1.length() && < s2.length(); i++) { c1 = (int) s1.tolowercase().charat(i); // see note 1 c2 = (int) s2.tolowercase().charat(i); // see note 1 comparing = c1 - c2; // see note 2 if(comparison != 0) // see note 3 homecoming comparison; } if(s1.length() > s2.length()) // see note 4 homecoming 1; else if (s1.length() < s2.length()) homecoming -1; else homecoming 0; }
notes:
i utilizetolowercase()
method convert strings lower case. if can't utilize "built-in" methods, can utilize conversion (that ascii += 32
piece). however, must careful: must check character value alphabetic character before converting them (use google find ascii table , check valid alphabetic character values) c1
, c2
integers, comparison = c1 - c2
hold difference between integers (characters). if comparison == 0
, means characters equal, nil done. if comparison != 0
, characters different; if comparison > 0
means c1
"bigger" c2
, s1
larger (look first paragraph of answer); if comparison < 0
s2
bigger. so, if comparison != 0
, can homecoming value. remember: return
sentence 2 things: returns value and exits function. execution of for
loop stopped too. the previous code solves issue minimum string length. but, posted in question, must deal case strings have different lengths. lastly part of code deals them (and can reached if comparison
still 0
): if s1.lenght() > s2.length()
, s1
"bigger" s2
, positive value (+1
) returned. if s1.lenght() < s2.length()
, s1
"smaller" s2
, negative value (-1
) returned. in other case, mean 2 things:
(a) chars of both strings equal (because comparison == 0
), ,
(b) length of both strings equal
strings are equal, , 0 value must returned.
java return compare
Comments
Post a Comment