PE1 (AY24/25)

Problems

3. Markup

This question utilizes the function is_match(), where the i position variable is crucial! After knowing this function, I rewrite my solution as follows

void markup(char *line)
{
  long line_len = (long)strlen(line);
  long times = 0;
  for (long i = 0; i < line_len; i += 1)
  {
    if (times >= 1)
    {
      if (is_match(line, (size_t)i, START))
      {
        i += 3-1;
        times += 1;
      }
      else if (is_match(line, (size_t)i, END))
      {
        i += 4-1;
        times = 0;
      }
      else if (is_lower(line[i]))
      {
        putchar(to_upper(line[i]));
      }
      else
      {
        putchar(line[i]);
      }
      continue;
    }
    if (is_match(line, (size_t)i , START))
    {
      i += 3-1;
      times += 1;
    }
    else if (is_match(line, (size_t)i, END))
    {
      i += 4-1;
      times = 0;
    }
    else
    {
      putchar(line[i]);
    }
  }
}

The idea is to set a variable times indicating we have encountered an open <U> and we should start printing the upper case letters. And once we encounter a closing </U>, we set our times to be 0. And notice that the awesome point of the position i is that we deal with one character at a time!

Last updated