;

C# Program to Find the Frequency of occurrences of the given Word in a given Sentence


Tutorialsrack 01/12/2019 C# Programs

In this C# program, we will learn how to write a program to find the frequency of occurrences of the given word in a given sentence.

Here is the code of the program to find the frequency of occurrences of the given word in a given sentence:

Code - C# Program To Find the Frequency of occurrences of the given Word in a given Sentence
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* C# Program to Find the Frequency or occurrences of the given Word in a given Sentence  */
        static void Main(string[] args)
        {
            string str, word;
            Console.Write("Enter the String: ");
            str = Console.ReadLine();
            Console.Write("\n");
            Console.Write("Enter the Word to Find the Frequency in a given Sentence: ");
            word = Console.ReadLine();
            Console.Write("\n");
            Console.WriteLine("No. of Occurrence of '"+word+"' word in a given sentence: {0}",Count_Word_Occurrences(str, word));
            Console.ReadKey();
        }

        public static int Count_Word_Occurrences(string sentence, string word)
        {
            int count = 0;
            int i = 0;
            while ((i = sentence.IndexOf(word, i)) != -1)
            {
                i += word.Length;
                count++;
            }
            return count;
        }
    }
}
Output

Enter the String: Program to Find the Frequency or occurrences of the given Word in a given Sentence

Enter the Word to Find the Frequency in a given Sentence: the

No. of Occurrence of 'the' word in a given sentence: 2


Related Posts



Comments

Recent Posts
Tags