To read our expectations for code in this course, read C Standards
Open your WebTop in Entainer.
Run cs1010, select Download CS1010 content, then choose Volumes and Volume 7.
Work in the downloaded Volume 7 directory.
Write your answers in student.c inside each exercise’s directory.
We handle input and output for you.
You may assume that allocations succeed.
Preface
We’re opting to give fewer questions for this volume as a 1-week respite post PE. We know it’s been a long week with the quizzes and exams, and also another EPP quiz this Saturday. We will soon release another practice set with more questions in case you guys want to have more practice. But as far as the volume goes, it will only be 2 questions for this week.
0. Histogram
Focus
Count characters, allocate an array of structs, and return its size through a pointer.
Introduction
A histogram records how often each item appears. Here, we want to count the letters in a string. For example, "ada" contains two as and one d, so its histogram has two records: {'a', 2} and {'d', 1}.
Return a heap-allocated array with one CharacterCount for each distinct character in str.
Set *nptr to the number of records, replacing its old value.
The input contains only lowercase English letters a through z.
Return records in alphabetical order, omitting letters with zero occurrences.
Do not change str.
For an empty string, set *nptr to zero and return nullptr.
Both pointer parameters are valid, and each character count fits in unsigned int.
We print the records and free the returned array.
You do not need to print or free it yourself.
Running and Testing
To compile: run make in 0-histogram
To test: run make test
To run: run ./histogram, type one line of lowercase letters, then press Enter
A blank input line is an empty string and produces no output.
Each output line contains a character, a space, and its count.
Examples
Example 1
Input
ada
Output
a 2d 1
There are two distinct letters, so *nptr is 2.
The array contains two records even though the input has three characters.
Example 2
Input
zaza
Output
a 2z 2
The records are in alphabetical order, regardless of which letter appears first.
1. Line Breaks
Focus
Allocate an array of pointers into an existing string, then free only the array you own.
Introduction
A string can contain several lines separated by newline characters. We want a list of pointers to the starts of those lines, without copying the text into new strings.
For example, "red\ngreen\nblue" has three lines: red, green, and blue. Their starts are at offsets 0, 4, and 10 in the original string. We promise that all lines will have at least one character, so there will not be consecutive newline characters. We also promise that the string will not start nor end on newlines. We also promise the string is never empty.
line points to a valid null-terminated string. Return a heap allocated array of pointers,
where element i points to the start of line i in the original string. Set *nptr to the number of lines, replacing its old value. The count pointer is valid.
Do not modify the original string or copy its characters into new strings.
A final line without a newline still counts as a single line.
Implement free_line_list to free only the allocated pointer array.
The individual pointers refer to borrowed text, so you must not free them or the original string. The freeing function must accept nullptr.
We call this function after printing the result.
Running and Testing
To compile: run make in 1-line-breaks
To test: run make test
To run the example: run ./line-breaks
To provide your input, type in your multiple lines and immediately follow it with the ’$’ character. For example:
redgreenblue$
Then hit enter.
Note: Do not put $ on its own line, this is treated as a trailing newline.
Examples
Original string
Number of lines
Pointer offsets
"red\ngreen\nblue"
3
0, 4, 10
"red\nblue"
2
0, 4
"adsf\nadsf"
2
0,5
"aoeu"
1
0
Our program will print based on the list of pointers you’ve returned.