# Bannorð

## Question

{% embed url="<https://open.kattis.com/problems/bannord>" %}

## 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](/coding/baisc-knowledge/c/library.md). 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 [Broken mention](broken://spaces/KipySCGxC8NC1UpA24DS/pages/JDGrXPJbEXCdWExTJxhm#pointer-arithmetic) to get the position of this letter in our line. And call the `replace()` function to replace the entire word with `*`.

{% code lineNumbers="true" %}

```c
void find_and_replace(char forbid_letter, char *line)
{
  char *first_occur_ptr = strchr(line, forbid_letter);
  while(first_occur_ptr != NULL)
  {
    long pos = first_occur_ptr - line;
    replace(line, pos);
    first_occur_ptr = strchr(line, forbid_letter);
  }
}
```

{% endcode %}

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:

{% code lineNumbers="true" %}

```c
void replace(char *line, long pos)
{
  // Left replacement
  for (long i = 0; pos-i >= 0 && line[pos-i] != WHITESPACE; i += 1)
  {
    line[pos-i] = REPLACE;
  }
  // Right replacement
  for (long i = 1; line[pos+i] != '\n' && line[pos+i] != WHITESPACE; i += 1)
  {
    line[pos+i] = REPLACE;
  }
}   
```

{% endcode %}

{% hint style="info" %}
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 [Broken mention](broken://spaces/KipySCGxC8NC1UpA24DS/pages/NbAW5YMUPwX7H0qLbo9R#short-circuiting) reasons.
{% endhint %}

### Code

{% @github-files/github-code-block url="<https://github.com/mendax1234/Coding-Problems/blob/main/kattis/bannord/bannord.c>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wenbo-notes.gitbook.io/coding/kattis/easy/bannord.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
