Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Number of letters and digits in a given string

C# Sharp Basic: Exercise-86 with Solution

Write a C# Sharp program to get the number of letters and digits in a given string.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text;
            text = "Python 3.0";
            Console.WriteLine("Original string:: "+text);
            Console.WriteLine(test(text));
            text = "dsfkaso230samdm2423sa";
            Console.WriteLine("\nOriginal string:: " + text);
            Console.WriteLine(test(text));
        }
        public static string test(string text)
        {
            int ctr_letters = text.Count(char.IsLetter);
            int ctr_digits = text.Count(char.IsDigit);            
            return "Number of letters: " + ctr_letters + "  Number of digits: " + ctr_digits;
        }
    }
}

Sample Output:

Original string:: Python 3.0
Number of letters: 6  Number of digits: 2

Original string:: dsfkaso230samdm2423sa
Number of letters: 14  Number of digits: 7

Flowchart:

Flowchart: C# Sharp Exercises - Number of letters and digits in a given string.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to find the cumulative sum of an array of number.
Next: Write a C# Sharp program to reverse a boolean value.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.