If you have a given string and you want to count the number of characters which are repeated in the string then you can use the following program.
public static void Main(string[] args) { int numberOfRepeatedChars=0; char[] chars=new char[25]; string str="AAABCDEE"; char tocheck=(char)'a'; foreach(var i in str) { int charNo=0; var asc=(char)i; charNo++; if(!chars.Contains((asc))) { chars[charNo]=asc; } else { numberOfRepeatedChars++; } } Console.WriteLine(numberOfRepeatedChars); }
the above program will print 2 as there are two characters which are repeated in the string which are A and E.
Leave a Reply