ExerciseFocus
0. Delete RowFree one string and move the remaining pointers
1. Deep CopyAllocate an independent copy of a linked list
2. Parse CommandBuild a struct containing separately allocated data
3. List FlatteningTurn a linked list into a string
4. Find and ReplaceSearch and copy into a new string

Getting Started

You may assume all allocations succeed.

0. Delete Row

Focus

Free an owned string while preserving the other strings and their pointers.

Introduction

You are given an array whose elements are pointers to heap-allocated strings.
The function receives a char ** pointing to the first element.
Each string has its own heap allocation.
Delete the string at a specified index and shift the remaining strings up.

For example, deleting index 1 from ["first", "second", "third"]
should leave ["first", "third", nullptr].
The string "second" is freed.
The other two strings stay in their existing allocations.

Your Task

In 0-delete-row/student.c, implement:

void delete_row(char **lines, size_t num_lines, size_t idx_to_delete);

Free exactly the selected string.
Shift subsequent pointers one position towards the start,
then set the final array element to nullptr.
Do not copy, modify, reallocate, or free any retained string.
Do not allocate a replacement pointer array or free the existing one.

There is at least one string, and the deletion index is valid.
Strings may be empty or have equal contents, but their allocations are separate.
If there is only one string, free it and set lines[0] to nullptr.
We print the resulting array and free the remaining strings and outer array.

Running and Testing

  • Run make in 0-delete-row to compile
  • Run make test to test
  • Run ./delete-row to enter your own input

Enter the number of strings (1 through 128) on the first line,
then that many lines of text, then the zero-based deletion index on its own line.
A blank text line represents an empty string.

Examples

Example 1

Input

3
asdf
hmm hello
okbai
1

Output

line 0:[asdf]
line 1:[okbai]
line 2:(null pointer)

The pointer to "okbai" moves into slot 1; its characters are not copied.

Example 2

Input

1
only
0

Output

line 0:(null pointer)

The outer array still exists, but it no longer points to any string.


1. Deep Copy

Focus

Allocate an independent copy of a linked list.

Introduction

Copying a pointer does not copy the things that it points to. Assuming we have a linked list of nodes, with a pointer list representing the head, LinkedList *list2 = list does not copy anything except the pointer itself (i.e., we now have two pointers pointing at the same list of nodes.)

A deep copy has the same values in the same order as the original list,
but each node occupies its own space on the heap.
Changing or freeing one list must not affect the other.

In this exercise, we’re going to make a deep copy of a linked list, so we have two independent copies of every node in the list by the end.

Your Task

In 1-deep-copy/student.c, implement:

LinkedList *deep_copy(LinkedList *list);

The node type in student.h is:

typedef struct LinkedList {
    int value;
    struct LinkedList *tail;
} LinkedList;

Create and return a deep copy of list.
Every copied node must be newly allocated; none may be shared with the original list.
Do not change or free the original nodes.
For an empty list, return nullptr.
Lists have no cycles, and you may assume allocations succeed.

The original list must be unchanged and the copy must have the same values in the same order.
Every copied node must be distinct and separate from the original nodes.
We free the original list before printing the result and freeing the copy.
You can use debug_print_list in student.c to inspect node values and addresses, printed to stderr.

Running and Testing

  • To compile: run make in 1-deep-copy
  • To test: run make test
  • To run: run ./deep-copy, enter integers separated by spaces or commas, then press Enter

Values fit in int.
A blank input line represents an empty list.

Examples

Example 1

Input

1 2 3 4

Output

You successfully made a copy of the list. There are now two copies of it on heap! :)

The copied list has four new nodes containing 1, 2, 3, and 4 in that order.
Repeated values, such as 10 10 10, still need a separate copied node for each occurrence.


2. Parse Command

Focus

Parse an already-read command into a struct with independently allocated fields.

Introduction

We will consider a simplified Linux command of the form
./command-name, followed by optional flags.
For example, ./ls -a -l has command name ls and two flags, a and l.
Your task is to put the command name and flags into a struct.

Your Task

In 2-parse-command/student.c, implement:

Command parse_command(char const *line);

The type in student.h is:

typedef struct Command {
    char *command_str;
    char *flags;
    size_t num_flags;
} Command;

We read the complete input line for you.
line contains the command without its ending newline.
You do not need to read from standard input or print anything.
We promise the following format:

  • The line starts with ./
  • The command name contains one or more ASCII letters, digits, underscores, or hyphens
  • Each flag is one ASCII letter preceded by a hyphen
  • Exactly one space separates the command name and each flag
  • There are no leading or trailing spaces

Allocate command_str to hold a null-terminated copy of the command name,
without ./ or any flags.
Set num_flags to the number of flags.
When there are flags, allocate a separate character array containing their letters
in input order, without spaces or hyphens.
This array is not required to be null-terminated; its length is num_flags.
Preserve repeated flags and letter case.
When there are no flags, set flags to nullptr and num_flags to 0.

Do not modify or free line or return pointers into it.
We free it before printing the returned fields, then free both allocated fields.
The Command value itself is returned by value; it does not need a separate heap allocation.

Running and Testing

  • Run make in 2-parse-command to compile
  • Run make test to test
  • Run ./parse-command, enter one command line, and press Enter

Examples

Example 1

Input

./ls -l -a

Output

program name: ls
num flags: 2
flags:-l -a

Example 2

Input

./empty

Output

program name: empty
num flags: 0
flags:

Only the command name is allocated in this case.


3. List Flattening

Focus

Traverse a linked list and allocate a string.

Introduction

A linked list can hold a string one character at a time.
For example, the list 'c' -> 'a' -> 't' -> nullptr holds the characters of "cat".
We want to flatten the list into one ordinary, null-terminated string.

Your Task

In 3-list-flattening/student.c, implement:

char *flatten_list(LinkedList *list);

The node type in student.h is:

typedef struct LinkedList {
    char c;
    struct LinkedList *tail;
} LinkedList;

Return a newly allocated string containing its characters in the same order,
followed by a null terminator.
You may add helper functions in student.c.
Allocate the output string, not new list nodes.
For an empty list (nullptr), return an allocated empty string, not nullptr.
The list contains no null characters and has no cycles.
Do not change or free its nodes.
We print and free your returned string, then free the original list.
You may assume allocations succeed and the string size fits in size_t.

Running and Testing

  • To compile: run make in 3-list-flattening
  • To test: run make test
  • To run: run ./list-flattening, type a line of characters, then press Enter

Each character, including spaces and punctuation, becomes a node.
The final newline is not part of the list.
A blank line represents an empty list.

Examples

Example 1

Input

cat

Output

Flattened list:[cat]

The output string contains three characters and a null terminator.
For a blank input line, the program prints Flattened list:[].


4. Find and Replace

Focus

Search a string and construct an independently allocated result.

Introduction

You’ll be given three strings: str, find, and replacement.
If find occurs in str, replace it with replacement.
Otherwise, return a copy of the original string, unchanged.

For example, replacing "123" with "@@@@@@" in "asdf123!"
gives "asdf@@@@@@!".
The replacement can be longer or shorter than the text it replaces.

Your Task

In 4-find-and-replace/student.c, implement:

char *find_and_replace(char const *str, char const *find,
                      char const *replacement);

Return a newly allocated, null-terminated string containing the result.
All three input strings are nonempty.
We promise that find occurs either zero times or exactly once in str.
Matching is case-sensitive.
Do not modify or free any input string.

Even when there is no match, return an independent copy, not str itself.
We free the input strings before printing and freeing your result.
The returned allocation must have room for all result characters and the null terminator.

Running and Testing

  • Run make in 4-find-and-replace to compile
  • Run make test to test
  • Run ./find-and-replace, then enter str, find, and replacement on three separate lines

Examples

Example 1

Input

asdf123!
123
@@@@@@

Output

result: [asdf@@@@@@!]

Only the matching part is replaced; the surrounding characters stay in order.

Example 2

Input

asdf123
asDF
new_string

Output

result: [asdf123]

There is no case-sensitive match.
The result has the same contents as str, but occupies its own allocation.