static int maxchars = 256;
现在显示字符串中的重复字符。
string s = "welcometomywebsite!";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");}
上面,我们计算了字符的频率。下面的完整示例显示了相同的内容 -
示例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 = "welcometomywebsite!"; 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"); } }}
以上就是如何使用 c# 打印字符串中的重复字符?的详细内容。
