This is a C# Program to find the frequency of the word ʺtheʺ in a given sentence.
This C# Program Finds the Frequency of the Word ʺtheʺ in a given Sentence.
Here the frequency of ‘the’ in the given string is found.
Here is source code of the C# Program to Find the Frequency of the Word ʺtheʺ 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 ʺtheʺ 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, "the")); 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 “the” in a given sentence from the counting variable. While loop is used to check the IndexOf() function value is not equal to -1.
If the condition is true then execute the statement. Compute the summation of the value of ‘i’ variable with the value of ‘pattern.length’ variable and increment the value of count variable value by 1. Print the frequency of the word “the” in a given sentence.
Enter the String : we only loop once over the source, which reduces the cost of the method. 3
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy MCA Books
- Practice Computer Science MCQs
- Practice MCA MCQs
- Buy C# Books
- Apply for Computer Science Internship