1){ Console.WriteLine( "Character"+(char)i);&nbs"/> 1){ Console.WriteLine( "Character"+(char)i);&nbs">

Home  >  Article  >  Backend Development  >  How to print repeated characters in a string using C#?

How to print repeated characters in a string using C#?

王林
王林forward
2023-08-28 11:53:09838browse

如何使用 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 -

Example

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete