1){ Console.WriteLine( "Character"+(char)i);&nbs"/> 1){ Console.WriteLine( "Character"+(char)i);&nbs">
Home >Backend Development >C#.Net Tutorial >How to print repeated characters in a string using C#?
Set the maximum value of characters.
static int maxCHARS = 256;
Now displays repeated characters in strings.
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"); }
Above, we calculated the frequency of characters. The complete example below shows the same -
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"); } } }
The above is the detailed content of How to print repeated characters in a string using C#?. For more information, please follow other related articles on the PHP Chinese website!