सबसे पहले, स्ट्रिंग सेट करें -
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