Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday 15 March 2017

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...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...