Introduction
This blog explains how we can count the characters of a given string, using C#. Many interviewers ask this question and ask freshers to write the program. We can write a program using C# like below.
Programs
- using System;
- public class Program {
- public static void Main() {
- string strFirst;
- char charCout;
- int Count = 0;
- Console.Write("Enter Your String:");
- strFirst = Console.ReadLine();
- Console.Write("Enter Count Character:");
- charCout = Convert.ToChar(Console.ReadLine());
- Console.Write("Your Character Count:");
- foreach(char chr in strFirst) {
- if (chr == charCout) {
- Count++;
- }
- }
- Console.Write(Count);
- }
- }
Conclusion
I hope this really helps the freshers in their interview, and also it should help those who are newly learning C# programming.