This is a C# Program to find the frequency of the word ʺisʺ in a given Sentence.
This C# Program Finds the Frequency of the Word ʺisʺ in a given Sentence.
Here the frequency of ‘is’ in the given string is found.
Here is source code of the C# Program to Find the Frequency of the Word ʺisʺ in a given Sentence. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Find the Frequency of the Word ʺisʺ in a given Sentence */ using System; class program { public static void Main() { string s1; Console.WriteLine("Enter the String : "); s1 = Console.ReadLine(); Console.WriteLine(counting.CountStringOccurrences(s1, "is")); Console.ReadLine(); } } public static class counting { public static int CountStringOccurrences(string text, string pattern) { int count = 0; int i = 0; while ((i = text.IndexOf(pattern, i)) != -1) { i += pattern.Length; count++; } return count; } }
In this C# program, we are reading the string using ‘s1’ variable. The CountStringOccurrences() function is used to find the frequency of the word “is” in a given sentence from the counting variable.
While loop is used to check the value of IndexOf() function is not equal to -1. If the condition is true then execute the iteration of the loop. Compute the addition of the value of ‘i’ variable with value of ‘pattern.length’ variable and increment the value of ‘count’ variable. Print the frequency of the word “is” in a sentence.
Enter the String : Rose is a Flower 1
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice MCA MCQs
- Apply for C# Internship
- Check C# Books