Length Difference programming exercise.
There ware once a war in the ancient kingdoms of strings. And army of char arrays fought each other for a millennia. But not anymore! You my friend, you are chosen to write a function that will bring a peace, by programmatically solve all their battles. Your job is:
You are given two string arrays. Array X and array Y. Count the number of characters in X and Y and determine who have more and how much more. Then return the result in a nice string message.
Example: x = {"aaa","bb", "dd"} //a total of 7 characters y = {"aa", "f", "gg"} //a total of 5 characters "The winner is X for he have 2 more characters!"
public static string whoWins(string[] x, string[] y) { //lets declare two variables for counting characters. //old Fortran users can do it even with one variable //but anyway.. int a = 0; int b = 0; //we will use foreach to go trough every string //and this "Length" property every string have (in c#) //and count them foreach (var item in x) { a = a + item.Length; } foreach (var item in y) { b = b + item.Length; } //after that we will decide the result //and return it if (a > b) return "X wins! " + "Chars left: " + (a - b); else if(b > a) return "Y wins! " + "Chars left: " + (b - a); else return "Draw!"; }