CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Problems
  • 2. Thesis
  • Tips
Edit on GitHub
  1. Current Year Exam

PE2 (AY24/25)

Problems

2. Thesis

A classic string manipulation problem.

My solution is to insert the \n at the proper white space to make sure we've excluded the leading and trailing spaces.

void wrap(char *line, long limit)
{
  size_t len = strlen(line);
  long one_ahead_len = 0;
  long next_length = 0;
  for (long i = 0; i < (long)len;)
  {
    next_length = next_word_length(line, i);
    one_ahead_len += next_length;
    if (one_ahead_len > limit)
    {
      if (line[i] != ' ')
      {
        line[i-1] = '\n';
      }
      else
      {
        line[i] = '\n';
        i += 1;
      }
      one_ahead_len = 0;
      continue;
    }
    i += next_length;
  }
  print_line(line);
}

The idea is that, i represents the starting character of a word of a white space, one_ahead_len stores the length of the current line plus one word or white space . And every time, we first get the next word length, which will return 1) 1 if line[i] is a space 2) the length of the word if it is the start of a word. It is implemented as follows:

long next_word_length (char *line, long i)
{
  long len = 0;
  if (line[i] == ' ')
  {
    return 1;
  }
  while (line[i] != ' ' && line[i] != 0)
  {
    len += 1;
    i += 1;
  }
  return len;
}

Let's continue, if the curr_len is bigger than limit, then we need to wrap by replacing the white space character with \n character. Now, let's see Line 12 to Line 19, this indicates that there are two cases when we wrap the line, it is 1) either you are at the start of a word 2) or you are at the start of a white space. In the first case, we should replace the white space which is exactly one position before i (line[i-1]) with \n while in the second case, we should replace the line[i] with \n and increment i by 1 to let it point to the next starting position of a word.

After we have done setting our \n, we set one_ahead_len = 0 and skip the increment of i += next_length because we haven't started dealing with the word starting at i.

In the normal case where one_ahead_line <= limit, we don't need to do anything and just need to set i += next_length

Tips

  1. When doing string traversal using while loop, always add current character is not equal to \0 as one of its terminating condition!

PreviousPE1 (AY24/25)NextFinal (AY24/25)

Last updated 6 months ago