zoukankan      html  css  js  c++  java
  • 问题 E: C#统计字符出现的个数

    题目描述

    编写一个实例方法getCountChar方法。该方法参数有两个,第一个参数可以是字符串s,第二个参数为字符c,方法返回值为第二个参数在第一个参数中出现次数。例如,CountChar("6221982",'2')返回值为3。
    部分程序代码已经给出。
    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){
            //


            在此处填写代码


            //
            }
        }
    }
     
    提交时,请提交整个题目!!!

    输入

    一个字符串s,一个字符c

    输出

    该字符c在字符串s中的出现次数

    样例输入

    6221982
    2

    样例输出

    3

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace 统计字符串中数字字符的个数
    {
     
        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 num = 0;
                for (int i = 0; i < s.Length; ++i)
                {
                    if (s[i] == c)
                    {
                        num++;
                    }
                }
                return num;
     
            // 
            }
        }
    }
    

      

  • 相关阅读:
    第一章 管理程序流(In .net4.5) 之 实现多线程和异步处理
    第十三章 接口
    第十二章 泛型
    第十一章 事件
    SqlServer杀死所有正在使用中的进程
    SqlServer数据库一直显示“正在还原”问题
    大叔学Spring Boot笔记(二)基本概念
    大叔学Spring Boot笔记(一)初识Sprint Boot
    SqlServer中的对象类型代码
    SqlServer将Json串转成表结果
  • 原文地址:https://www.cnblogs.com/mjn1/p/12576473.html
Copyright © 2011-2022 走看看