c# - How do i turn this into a for loop? -
hey im wondering how turn loop, don't have copy 26 times each letter of alphabet:
count[1] = str.split('a').length - 1; console.writeline("a comes x " + count[1]);
count blank integer array takes 26 values, represent amount of times letter of alphabet in string str, stored in string array called letters
instead of splitting , creating new array, use count()
function. can loop through each character, or loop existing array.
for (char c = 'a'; c <= 'z'; c++) { console.writeline(c + " comes x " + str.count(x => char.toupper(x) == c)); }
Comments
Post a Comment