मान लें कि हमारा स्ट्रिंग है -
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