먼저 문자열을 −
로 설정합니다.string str = "Website";
Console.WriteLine("String: "+str); 문자열의 모든 문자를 확인하고 해당 문자의 발생 횟수를 계산할 변수를 증가시키십시오 -
for (int j = 0; j < str.Length; j++) {
if (str[0] == str[j]) {
cal++;
}
} 예시
다음 코드를 실행하여 각 문자의 발생 횟수를 계산할 수 있습니다.
using System;
public class Demo {
public static void Main() {
string str = "Website";
Console.WriteLine("String: "+str);
while (str.Length > 0) {
Console.Write(str[0] + " = ");
int cal = 0;
for (int j = 0; j < str.Length; j++) {
if (str[0] == str[j]) {
cal++;
}
}
Console.WriteLine(cal);
str = str.Replace(str[0].ToString(), string.Empty);
}
Console.ReadLine();
}
} 출력
String: Website W = 1 e = 2 b = 1 s = 1 i = 1 t = 1