Q:

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

belongs to collection: C# Basic Programs | String programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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'))
            {
                count++;
            }
        }
        Console.WriteLine("Number of Digits in the string: "+ count);
    }
}

Output:

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

Explanation:

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | String programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to concatenate the two strings using a ... >>
<< C# program to print the list of all possible subst...