Программа C# для оценки частоты слова «есть» в предложении

Опубликовано: 12 Сентября, 2022

Получив строку в качестве входных данных, нам нужно написать программу на C# для подсчета частоты слова «is» в строке. Задача программы — подсчитать количество вхождений заданного слова «is» в строку и вывести количество вхождений «is».

Примеры:

Input : string = “The most simple way to prepare for interview is to practice on best computer science portal that is GeeksforGeeks”

Output : Occurrences of “is” = 2 Time

Input : string = “is this is the way to talk to your sister? I don’t know what that is”

Output : Occurrences of “is” = 3 Time

Explanation: The “is” has also occurred in the word “sister” and “this” but we are looking for the word “is” to occur separately. Hence only 3 time.

Использование итерационных методов

Подход:

  1. Split the string by spaces
  2. Store all the words in an array of strings.
  3. Now run a loop at 0 to the length of the array of string and check if our string is equal to the word “is”.
  4. If the condition is true increment the count else do not increment it.

Код:

Выход:

3

Временная сложность: O(n), где n — длина строки.