String based program

String program #6 (Count frequency of character in string)

Enter string: Imagination hunt
Enter any character from string to count frequency: n
Repetition of n is: 3 times

The program for the string is written in C# programming language and will accept a string and a character as input. The logic is to count the input character frequency from string.
 
String based program

Note: If you are new to C# and Console Application. Try to code First C# Program

Note: Read articles on how to use Loops and Conditions.

Let’s find out a simple and easy way to code the program.

Practical Implementation:

using System;

namespace patternProblem.String
{
    class String6     //count frequencey of character in string
    {
        static void Main(string[] args)
        {
            Console.Write("Enter string: ");
            string str = Console.ReadLine();
            Console.Write("Enter any character from string to count frequency: ");
            char ch = Console.ReadKey().KeyChar;
            int count = 0;

            for (int i = 0; i < str.Length - 1; i++)
            {
                if (str[i] == ch)
                {
                    count++;
                }
            }
            if (count == 0)
            {
                Console.WriteLine("\nGiven character not found.");
            }
            else
            {
                Console.WriteLine("\nRepetition of {0} is: {1} times", ch, count);
            }

            Console.ReadKey();
        }
    }
}

Output:
The input string here is “Imagination hunt” and input character is n. So, based on the input character program will return the frequency of character from string. The output is shown below:

Enter string: Imagination hunt
Enter any character from string to count frequency: n
Repetition of n is: 3 times
Press any key to continue . . .


For any query, comment us below.

Skip to Main Table String based program

Previous - String based program #5


Click imagination hunt to read latest blogs.


Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions