The Program in C# Program to Count Specified Character (both cases) in Given String is given below:
using System;
namespace CharacterCounter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter a string here to count the characters: ");
string input = Console.ReadLine();
Console.WriteLine("Well,enter the character you want to count: ");
char character = Console.ReadLine()[0];
// Convert the input to lowercase
input = input.ToLower();
// Count the specified character (both cases)
int count = 0;
foreach (char c in input)
{
if (c == character || c == char.ToUpper(character))
{
count++;
}
}
// Display the result
Console.WriteLine("Okay, the character '" + character + "' appears " + count + " times in the input string.");
Console.ReadKey();
}
}
}
Output:
Hello Codeauri Family,enter a string here to count the characters: twenty
Well,enter the character you want to count:
twenty
Okay, the character ‘t’ appears 2 times in the input string.
Pro-Tips💡
Here are the step by step execution of above program:
- The
using System;
statement imports theSystem
namespace, which provides access to various basic and fundamental classes. - The
namespace CharacterCounter
declaration creates a new namespace with the nameCharacterCounter
. - The
class Program
declaration creates a new class with the nameProgram
. This class contains the main entry point of the program. - The
static void Main(string[] args)
method is the entry point of the program. It’s called when the program starts. - The line
Console.WriteLine("Hello Codeauri Family,enter a string here to count the characters: ");
prints a message to the console, asking the user to enter a string. - The line
string input = Console.ReadLine();
reads the input string from the user. - The line
Console.WriteLine("Well,enter the character you want to count: ");
prints a message to the console, asking the user to enter the character they want to count. - The line
char character = Console.ReadLine()[0];
reads the input character from the user and stores it in thecharacter
variable. The[0]
index accesses the first character of the input string. - The line
input = input.ToLower();
converts the input string to lowercase using theToLower
method. - The
foreach (char c in input)
loop iterates through each character in the input string. Thec
variable stores the current character. - The line
if (c == character || c == char.ToUpper(character))
checks if the current character is equal to the input character or its uppercase version. If this condition is true, thecount
variable is incremented. - The line
Console.WriteLine("Okay, the character '" + character + "' appears " + count + " times in the input string.");
displays the result of the character count. - The line
Console.ReadKey();
waits for the user to press any key to exit the program.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.