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

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

王林
Release: 2023-08-28 11:53:09
forward
937 people have browsed it

如何使用 C# 打印字符串中的重复字符?

Set the maximum value of characters.

static int maxCHARS = 256;
Copy after login

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");
}
Copy after login

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");
      }
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template