Answer to Question #238490 in C# for Gayatri

Question #238490
Write a program to take input of string from user and count the number of uppercase alphabets, lowercase alphabets, digits and special characters. Store these counts in an array of size 4.

Description

1. Take console input of string, in Main () method, from user and store.

2. Pass the string to method 'int[] CategorizeCharactersFromString (string sourceStr) and write the logic in that method to count the different types of characters.

3. Return the count from int[] CategorizeCharactersFromString (string sourceStr) to Main() and display the values.

4. Template Code of the same is provided at:

Project Directory/NamespaceName/dotnet run
1
Expert's answer
2021-09-17T13:09:27-0400
using System;
using System.Linq;

namespace ConsoleApplication1
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var str = System.Console.ReadLine();
            var result = CategorizeCharactersFromString(str);
            foreach (var t in result)
            {
                Console.WriteLine(t);
            }
        }

        private static int[] CategorizeCharactersFromString(string sourceStr)
        {
            const string specials = "~!@#$%^&*()_+{}:\"<>?";
            
            var upperCount = sourceStr.Count(char.IsUpper);
            var lowerCount = sourceStr.Count(char.IsLower);
            var digitsCount = sourceStr.Count(char.IsDigit);
            var specialsCount = sourceStr.Count(x => specials.Contains(x));

            return new[] {upperCount, lowerCount, digitsCount, specialsCount};
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS