Basic Programming C# Examples

C# Program to Count Specified Character (both cases) in Given String

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:

  1. The using System; statement imports the System namespace, which provides access to various basic and fundamental classes.
  2. The namespace CharacterCounter declaration creates a new namespace with the name CharacterCounter.
  3. The class Program declaration creates a new class with the name Program. This class contains the main entry point of the program.
  4. The static void Main(string[] args) method is the entry point of the program. It’s called when the program starts.
  5. 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.
  6. The line string input = Console.ReadLine(); reads the input string from the user.
  7. 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.
  8. The line char character = Console.ReadLine()[0]; reads the input character from the user and stores it in the character variable. The [0] index accesses the first character of the input string.
  9. The line input = input.ToLower(); converts the input string to lowercase using the ToLower method.
  10. The foreach (char c in input) loop iterates through each character in the input string. The c variable stores the current character.
  11. 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, the count variable is incremented.
  12. The line Console.WriteLine("Okay, the character '" + character + "' appears " + count + " times in the input string."); displays the result of the character count.
  13. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X