Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Word Finder | Fundamentals of Strings
C# Strings & Text Processing

bookChallenge: Word Finder

After learning how to search for and count occurrences of characters and substrings in previous chapters, you are now ready to tackle a classic text processing challenge: finding out how many times a specific word appears in a paragraph. This is more nuanced than simply counting substrings, because you must ensure you only count whole words. For example, if you are searching for the word "the" in the sentence "There is a theme," only the standalone word "the" should be counted, not the "the" in "theme" or "There".

Task

Write a function that counts how many times a specific word appears in a given paragraph, considering only whole word matches. The function should not count instances where the search word is part of another word.

  • Return the number of times word appears as a whole word in paragraph;
  • The search should not be case-sensitive;
  • Only exact, whole word matches should be counted;
  • If paragraph or word is empty, return 0.

Starter Code

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp
{
    public class Program
    {
        public static int CountWordOccurrences(string paragraph, string word)
        {
            // Write your code here
        }

        public static void Main(string[] args)
        {
            int result1 = CountWordOccurrences("The quick brown fox jumps over the lazy dog. The dog barked.", "the");
            Console.WriteLine(result1);

            int result2 = CountWordOccurrences("There is a theme here, but the theme is subtle.", "the");
            Console.WriteLine(result2);

            int result3 = CountWordOccurrences("Hello world! Hello again, world.", "world");
            Console.WriteLine(result3);

            int result4 = CountWordOccurrences("No matches here.", "absent");
            Console.WriteLine(result4);

            int result5 = CountWordOccurrences("", "any");
            Console.WriteLine(result5);
        }
    }
}

Unit Test Checklist

  • Returns the correct count for a word appearing as a standalone word, regardless of case;
  • Does not count the word when it is part of another word;
  • Returns 0 if the word does not appear in the paragraph;
  • Returns 0 if the paragraph is empty;
  • Returns 0 if the word is empty.
Tehtävä

Swipe to start coding

Write a function that counts how many times a specific word appears in a given paragraph, considering only whole word matches. The function should not count instances where the search word is part of another word.

  • Return the number of times word appears as a whole word in paragraph.
  • The search should not be case-sensitive.
  • Only exact, whole word matches should be counted.
  • If paragraph or word is empty, return 0.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 7
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you show me how to implement the CountWordOccurrences function?

Can you explain how to use regular expressions to match whole words in C#?

Can you provide more test cases to ensure the function works correctly?

close

bookChallenge: Word Finder

Pyyhkäise näyttääksesi valikon

After learning how to search for and count occurrences of characters and substrings in previous chapters, you are now ready to tackle a classic text processing challenge: finding out how many times a specific word appears in a paragraph. This is more nuanced than simply counting substrings, because you must ensure you only count whole words. For example, if you are searching for the word "the" in the sentence "There is a theme," only the standalone word "the" should be counted, not the "the" in "theme" or "There".

Task

Write a function that counts how many times a specific word appears in a given paragraph, considering only whole word matches. The function should not count instances where the search word is part of another word.

  • Return the number of times word appears as a whole word in paragraph;
  • The search should not be case-sensitive;
  • Only exact, whole word matches should be counted;
  • If paragraph or word is empty, return 0.

Starter Code

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp
{
    public class Program
    {
        public static int CountWordOccurrences(string paragraph, string word)
        {
            // Write your code here
        }

        public static void Main(string[] args)
        {
            int result1 = CountWordOccurrences("The quick brown fox jumps over the lazy dog. The dog barked.", "the");
            Console.WriteLine(result1);

            int result2 = CountWordOccurrences("There is a theme here, but the theme is subtle.", "the");
            Console.WriteLine(result2);

            int result3 = CountWordOccurrences("Hello world! Hello again, world.", "world");
            Console.WriteLine(result3);

            int result4 = CountWordOccurrences("No matches here.", "absent");
            Console.WriteLine(result4);

            int result5 = CountWordOccurrences("", "any");
            Console.WriteLine(result5);
        }
    }
}

Unit Test Checklist

  • Returns the correct count for a word appearing as a standalone word, regardless of case;
  • Does not count the word when it is part of another word;
  • Returns 0 if the word does not appear in the paragraph;
  • Returns 0 if the paragraph is empty;
  • Returns 0 if the word is empty.
Tehtävä

Swipe to start coding

Write a function that counts how many times a specific word appears in a given paragraph, considering only whole word matches. The function should not count instances where the search word is part of another word.

  • Return the number of times word appears as a whole word in paragraph.
  • The search should not be case-sensitive.
  • Only exact, whole word matches should be counted.
  • If paragraph or word is empty, return 0.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 7
single

single

some-alt