1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | //Compare two strings by comparing the sum of their values (ASCII character code). //For comparing treat all letters as UpperCase. // //Null-Strings should be treated as if they are empty strings. //If the string contains other characters than letters, treat the whole string as it would be empty. // //Examples: // //"AD","BC" -> equal // //"AD","DD" -> not equal // //"gf","FG" -> equal // //"zz1","" -> equal // //"ZzZz", "ffPFF" -> equal // //"kl", "lz" -> not equal // //null, "" -> equal // //Your method should return true, if the strings are equal and false if they are not equal. package Suss; public class CodrWar_5 { // best! // public static boolean compare(String s1, String s2) { // // if (s1 == null || !s1.matches("[a-zA-Z]+")) s1 = ""; // if (s2 == null || !s2.matches("[a-zA-Z]+")) s2 = ""; // // return s1.toUpperCase().chars().sum() == s2.toUpperCase().chars().sum(); // } public static Boolean compare(String s1, String s2){ if( s1 == null ){ s1= ""; } if( s2 == null ){ s1= ""; } s1 = s1.toUpperCase(); s2 = s2.toUpperCase(); int sum_s1 =0; int sum_s2 =0; for(int i = 0 ; i<s1.length() ; i++){ int a =(int)s1.charAt(i); System.out.println(a); if( a < 65 || a > 90){ sum_s1 = 0; break; } sum_s1+=(int)s1.charAt(i); } System.out.println(s1+" "+sum_s1); for(int i = 0 ; i<s2.length() ; i++){ int a =(int)s2.charAt(i); System.out.println(a); if(a < 65 || a > 90){ sum_s1 = 0; break; } sum_s2+=(int)s2.charAt(i); } System.out.println(s2+" "+sum_s2); if(sum_s1 == sum_s2){ return true; } return false; } public static void main(String[] args) { //"ZzZz", "ffPFF" -> equal String s1 = "ZzZz"; String s2 = "ffPFF"; compare(s1, s2); } } |
20170801
2017. 8. 1. 10:39