C#: 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)
{
// Declaring a variable to hold text
string text;
// Assigning a value to the 'text' variable
text = "Python 3.0";
// Displaying the original string in the console
Console.WriteLine("Original string:: " + text);
// Calling the 'test' method and displaying the result
Console.WriteLine(test(text));
// Assigning a new value to the 'text' variable
text = "dsfkaso230samdm2423sa";
// Displaying the new original string in the console
Console.WriteLine("\nOriginal string:: " + text);
// Calling the 'test' method again and displaying the result
Console.WriteLine(test(text));
}
// Method to count the number of letters and digits in a string
public static string test(string text)
{
// Counting the number of letters in the given 'text' using LINQ
int ctr_letters = text.Count(char.IsLetter);
// Counting the number of digits in the given 'text' using LINQ
int ctr_digits = text.Count(char.IsDigit);
// Returning the count of letters and digits as a string
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:
C# Sharp Code Editor:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/basic/csharp-basic-exercise-86.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics