C# program to count the total number of digits in an alpha-numeric string

Here, we are going to learn how to count the total number of digits in an alpha-numeric string in C#?
Submitted by Nidhi, on October 12, 2020

Here, we will find the count of numbers in a specified string by checking each digit one by one.


The source code to count the total number of digits in an alphanumeric string is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to count the total number of 
//digits in the alpha-numeric string.

using System;

class Demo
    public static void Main()
        string str="";
        int count=0;

        Console.Write("Enter the string: ");
        str = Console.ReadLine();
        for (int i=0; i<str.Length; i++)
            if ((str[i] >= '0') && (str[i] <= '9'))
        Console.WriteLine("Number of Digits in the string: "+ count);


Enter the string: A124B27
Number of Digits in the string: 5
Press any key to continue . . .


Here, we created a class Demo that contains the Main() method. The Main() method is the entry point for the program,  here we created two local variables str and count. Then we read the value of the string and check each character of the string, if the character is a digit the increase the value of the count variable. That's why we finally got the count of all digits present in the input string.

C# Basic Programs »


What's New (MCQs)

Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.