C#程序设计练习题十五:问题 C: c#统计字符串中数字字符的个数

题目描述

假设有一个GetNumber方法(参数为字符串strSource),编写一个静态方法可以用来统计字符串strSource中数字字符的个数。

输入

输入一个字符串strSource

输出

strSource字符串中数字字符的个数

样例输入

asffkl8asjkfjklas3jdf9lkj!

样例输出

3

using System;
namespace ConsoleApplication1
{
    
    class Program
    {
        static void Main(string[] args)
        {
                
                string s=Console.ReadLine();
                GetNumber(s);
                Console.ReadKey(); 
        }
        public static void GetNumber(string strSource)
        {
            int num = 0;
            foreach (char c in strSource)
            {
                if (char.IsDigit(c))
                    num++;
            }
            Console.WriteLine(num);
        }
    }
}
发布了61 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/zhouchenghao123/article/details/105041762