Count char occurrence in string.

Write a program to count the number of occurrences of a character in String, do not consider case of char from string.

For example

Input: 

String =”india”
Char = ‘i’

Output
2

Solution:

using System;
using System.Linq;

namespace CountOccureance
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter string:");
            var str = Console.ReadLine();


            Console.WriteLine("Enter Char");
            var ch = Convert.ToChar(Console.ReadLine());


            var count = str.Count(x => char.ToUpperInvariant(x) == char.ToUpperInvariant(ch));

            Console.WriteLine($"{count} char present in given string");
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *