Bannorð

Question

Solution

Idea

This is another pretty awesome question about string manipulation! My idea is as follows:

  1. Iterate through each forbidden letter in the line

  2. replace the words with '*'

To implement this idea, I have used strchr() in the <string.h> from the Library. And one of the major functions find_and_replace() will do the work of finding the location of the first occurence of the forbidden letter. Use the property of strchr(), we will do the Pointer Arithmetic to get the position of this letter in our line. And call the replace() function to replace the entire word with *.

To replace the word with *, given that we know the position of the forbidden letter, we need to replace the left remaining and right remaining letters in the word. The implementation logic is shown as follows:

Notice that whenever do the string manipulation, use an index integer and pay attention to the edge cases. Usually, these edge cases should be put at the first part of the && operator for Short-Circuiting reasons.

Code

https://github.com/mendax1234/Coding-Problems/blob/main/kattis/bannord/bannord.c

Last updated