C# program counts the number of occurrences of each character

PHPz
Release: 2023-09-06 17:41:02
forward
1893 people have browsed it

C# program counts the number of occurrences of each character

First, set up the string -

string str = "Website";
Console.WriteLine("String: "+str);
Copy after login

Check each character in the string and add a variable to count the number of times that character appears-

for (int j = 0; j < str.Length; j++) {
   if (str[0] == str[j]) {
      cal++;
   }
}
Copy after login

Example

You can try running the following code to count the occurrences of each character.

Demo example

using System;
public class Demo {
   public static void Main() {
      string str = "Website";
      Console.WriteLine("String: "+str);
      while (str.Length > 0) {
         Console.Write(str[0] + " = ");
         int cal = 0;
         for (int j = 0; j < str.Length; j++) {
            if (str[0] == str[j]) {
               cal++;
            }
         }
         Console.WriteLine(cal);
         str = str.Replace(str[0].ToString(), string.Empty);
      }
      Console.ReadLine();
   }
}
Copy after login

Output

String: Website
W = 1
e = 2
b = 1
s = 1
i = 1
t = 1
Copy after login

The above is the detailed content of C# program counts the number of occurrences of each character. 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