문자열이 −
라고 가정해 보겠습니다.String s = "HeathLedger!";
이제 새 배열을 만드십시오.
int []cal = new int[maxCHARS];
새 메서드를 만들고 문자열과 새 배열을 모두 전달합니다. 캐릭터의 최대 출현 횟수를 찾습니다.
static void calculate(String s, int[] cal) {
for (int i = 0; i < s.Length; i++)
cal[s[i]]++;
} 전체 코드를 보자 -
예시
using System;
class Demo {
static int maxCHARS = 256;
static void calculate(String s, int[] cal) {
for (int i = 0; i < s.Length; i++)
cal[s[i]]++;
}
public static void Main() {
String s = "thisisit!";
int []cal = new int[maxCHARS];
calculate(s, cal);
for (int i = 0; i < maxCHARS; i++)
if(cal[i] > 1) {
Console.WriteLine("Character "+(char)i);
Console.WriteLine("Occurrence = " + cal[i] + " times");
}
}
} 출력
Character i Occurrence = 3 times Character s Occurrence = 2 times Character t Occurrence = 2 times