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.
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
Keep learning and sharing...
No comments:
Post a Comment