OJ Problem 3446 C# count the number of characters appearing

Title description

Write an instance method getCountChar method. This method has two parameters. The first parameter can be the string s, the second parameter is the character c, and the return value of the method is the number of times the second parameter appears in the first parameter. For example, CountChar("6221982",'2') returns a value of 3. 
Part of the program code has been given. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{     class Program     {         static void Main(string[] args)         {              string str = Console.ReadLine( );                  char ch = (char) Console.Read();             Program pro = new Program();             Console.WriteLine(pro.getCountChar(str,ch));             //Console.ReadKey();         }










        public int getCountChar(String s,char c){         //          Fill in the code here         //          }     } }





When submitting, please submit the entire question! !

enter

A string s, a character c

Output

The number of occurrences of the character c in the string s

Sample input

6221982
2

Sample output

3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            char ch = (char)Console.Read();
            Program pro = new Program();
            Console.WriteLine(pro.getCountChar(str, ch));
            //Console.ReadKey();
        }
        public int getCountChar(String s,char c){
            int count = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == c)
                {
                    count++;
                }
            }
            return count;
        }
    }
} 

 

Guess you like

Origin blog.csdn.net/wangws_sb/article/details/105112028