I'm stuck at some interesting task. I have 3 strings (hello, heavy & word). Need to calculate sum of each wold and print the biggest world and sum. For calculating - a = 1, z = 26. So hello = 50, heavy = 61 & word = 60. The biggest string is "heavy" and I need to print it like "heavy, 61". I found code who calculate chars from one string:
String[] words = {"hello", "heavy", "word"};
String str = "abc";
int sum = 0;
for (char ch : str.toCharArray()) {
if (ch >= 'a' && ch <= 'z') {
sum += 1 + ch - 'a';
}
}
System.out.printf("%s %d%n", str, sum); //the result is abc, 6
The result from this code is: abc, 6
The question is, how to calculate all words and how to print the biggest one? I can write sum for each word and calculate it later, but in my task words are random from console. Scanning isn't problem.
Edit: All words are lowercase letters.