DPST1091 Revision Strings Exercises

Revision Exercise: Count the number of words in a string

Write a C program word_count.c which reads characters from its input until the end of input and then prints a count of the characters, words and lines it read.

Assume the only non-word characters are space and newline.

All other characters should be considered word characters.

A word is defined as a maximal non-empty sequence of word characters.

Multiple spaces or newline characters may separate words.

Match the output format below exactly:

dcc -o word_count word_count.c
./word_count
The quick brown fox jumped over the lazy dog.

1 lines 9 words 46 characters
./word_count
only
    three
        words

3 lines 3 words 29 characters
./word_count
Are you saying 'Boo' or 'Boo-Urns'?

1 lines 6 words 36 characters
./word_count
 In this house,
we obey the laws of thermodynamics!

2 lines 9 words 52 characters
./word_count
Simpson, Homer Simpson
He's the greatest guy in history
 From the
  Town of Springfield
  He's about to hit a chestnut tree.

5 lines 21 words 125 characters

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest word_count

Revision Exercise: Reverse the lines of text on input

Write a C program reverse_lines.c which reads lines and writes them out with the characters of each line in reverse order. It should stop when it reaches the end of input.

For example:

dcc -o reverse_lines reverse_lines.c
./reverse_lines
Two roads diverged in a yellow wood,
,doow wolley a ni degrevid sdaor owT
And sorry I could not travel both
htob levart ton dluoc I yrros dnA
And be one traveler, long I stood
doots I gnol ,relevart eno eb dnA
To where it bent in the undergrowth;
;htworgrednu eht ni tneb ti erehw oT
You don't make friends with salad.
.dalas htiw sdneirf ekam t'nod uoY


You can assume lines will contain at most 256 characters.

You can assume lines are terminated with a newline ('\n') character,

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest reverse_lines

Revision Exercise: String Length

For this activity, you'll be writing the function string_length. It takes a string, and finds its length, excluding the null-terminator.

Download string_length.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/25T1/activities/string_length/string_length.c .

Your task is to add code to this function in string_length.c:

// Takes a string and finds its length, excluding the null-terminator.
int string_length(char *string) {
    // Your code goes here!
    // Don't forget to return your result.
    return 0;
}
string_length.c also contains a simple main function with some simple assert-based tests to help you build your solution:
int main(int argc, char *argv[]) {

    // Some simple assert-based tests.
    // You probably want to write some more.
    assert(string_length("") == 0);
    assert(string_length("!") == 1);
    assert(string_length("Hello, world!") == 13);
    assert(string_length("17... seventeen.\n") == 17);

    printf("All tests passed.  You are awesome!\n");

    return 0;
}

Your string_length function will be called directly in marking. The main function is only to let you test your string_length function

You can add more assert tests to main to test your string_length function.

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest string_length

Revision Exercise: Show Letters

In this activity, you will need to complete a function that prints an array of char values.

You should use printf("%c") to print each character in the array.

Download show_letters.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/25T1/activities/show_letters/show_letters.c .

Your task is to add code to this function in show_letters.c:

// print size characters from array letters
void show_letters(int size, char letters[]) {
    // Put your code here
}
show_letters.c also contains a main function to help you test your function Your show_letters function will be called directly in marking. The main function is only to let you test your show_letters function

Here is how show_letters.c should behave after you add the correct code to the function show_letters:

dcc show_letters.c -o show_letters
./show_letters
Hello, world!
Test message.
sample text
a string

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest show_letters

Revision Exercise: Show Terminated Letters

In this activity, you will need to complete a function that prints an array of char values.

You should use printf("%c") to print each character in the array.

Your function will not be given the number of characters to show, instead, you must display characters from the array until you find a null terminator (0 or '\0') in the array.

Download show_terminated_letters.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/25T1/activities/show_terminated_letters/show_terminated_letters.c .

Your task is to add code to this function in show_terminated_letters.c:

void show_terminated_letters(char *letters) {
    // Your code here
}
show_terminated_letters.c also contains a main function to help you test your function Your show_terminated_letters function will be called directly in marking. The main function is only to let you test your string_reverse function

Here is how string_reverse.c should behave after you add the correct code to the function string_reverse:

dcc show_terminated_letters.c -o show_terminated_letters
./show_terminated_letters
Hello, world!
Test message.
sample text
a string

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest show_terminated_letters

Revision Exercise: String Copy

For this activity, you'll be writing the function string_copy

It takes a string in the source buffer, and copies it to the destination buffer, which is dest_size elements in size. If there are more characters in source than there is array space in destination, you should stop after you have filled the array. You should always make sure that your function null-terminates the destination array.

Download string_copy.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/25T1/activities/string_copy/string_copy.c .

Your task is to add code to this function in string_copy.c:

// Takes a string in `source`, and copies it to `destination`, which
// is `destSize` elements in size; only copies up to `destSize` bytes.
// Ensures the `destination` array is null-terminated.
void string_copy(char *destination, char *source, int destination_size) {
    // Put your code here
}
string_copy.c also contains a simple main function with to help you test your string_copy function
int main(int argc, char *argv[]) {
    // Declare a buffer.  In this case, we're declaring and using a
    // 64-byte buffer, but this could be any length you like, and in
    // our tests you will be required to handle arrays of any length.
    char buffer[BUFFER_LENGTH] = {0};

    // Copy a string into the buffer ...
    string_copy(buffer, "Seventeen bytes.\n", BUFFER_LENGTH);

    // ... and print it out.  The `%s` format code prints a string.
    printf("<%s>\n", buffer);

    return 0;
}

Your string_copy function will be called directly in marking. The main function is only to let you test your string_copy function

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest string_copy

Revision Exercise: String To Upper

For this activity, you'll be writing the function string_to_upper. It takes a string and converts it to upper case

Download string_to_upper.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/25T1/activities/string_to_upper/string_to_upper.c .

Your task is to add code to this function in string_to_upper.c:

// Convert the characters in `buffer` to upper case
void string_to_upper(char *buffer) {
    // YOUR CODE GOES HERE!
}
string_to_upper.c also contains a simple main function to help you test your solution.
int main(int argc, char *argv[]) {

    // NOTE: THIS WON'T WORK:
    // char *str = "Hello!"
    // string_to_upper(str)
    //
    // str only points to a string literal, which it is not legal to change.
    // If you attempt to modify it on Linux you will get a runtime error.
    // Instead, you need to create an array to store the string in, e.g.:
    //
    // char str[] = "Hello!"
    // string_to_upper(str)

    char str[] = "Seventeen...  SEVENTEEN, I SAY!";
    string_to_upper(str);
    printf("%s\n", str);
    return 0;
}

Your string_to_upper function will be called directly in marking. The main function is only to let you test your string_to_upper function

int main(int argc, char *argv[]) {

    // NOTE: THIS WON'T WORK:
    // char *str = "Hello!"
    // string_to_upper(str)
    //
    // str only points to a string literal, which it is not legal to change.
    // If you attempt to modify it on Linux you will get a runtime error.
    // Instead, you need to create an array to store the string in, e.g.:
    //
    // char str[] = "Hello!"
    // string_to_upper(str)

    char str[] = "Seventeen...  SEVENTEEN, I SAY!";
    string_to_upper(str);
    printf("%s\n", str);
    return 0;
}

Your string_to_upper function will be called directly in marking. The main function is only to let you test your string_to_upper function

Here is how string_to_upper.c should behave after you add the correct code to the function string_to_upper:

dcc string_to_upper.c -o string_to_upper
./string_to_upper
SEVENTEEN...  SEVENTEEN, I SAY!

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest string_to_upper

Revision Exercise: Rotate Thirteen

For this activity, you'll be writing the function rotate_thirteen. It takes a string, and rotates each character by thirteen positions in the alphabet.

To see more about this algorithm, check out the wikipedia article.

Download rotate_thirteen.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/25T1/activities/rotate_thirteen/rotate_thirteen.c .

Your task is to add code to this function in rotate_thirteen.c:

void rotate_thirteen(char *string) {
    // YOUR CODE HERE
    // See: https://en.wikipedia.org/wiki/ROT13
}
rotate_thirteen.c also contains a simple main function with some simple assert-based tests to help you build your solution:
int main(int argc, char *argv[]) {
    // Example tests
    char test_a[MAX_LENGTH] = "Hello, world!";
    rotate_thirteen(test_a);
    assert(strings_equal("Uryyb, jbeyq!", test_a));

    char test_b[MAX_LENGTH] = "abcdefghijklmnopqrstuvwxyz";
    rotate_thirteen(test_b);
    assert(strings_equal("nopqrstuvwxyzabcdefghijklm", test_b));

    char test_c[MAX_LENGTH] = "The quick brown fox jumped over the lazy dog.";
    rotate_thirteen(test_c);
    assert(strings_equal("Gur dhvpx oebja sbk whzcrq bire gur ynml qbt.", test_c));

    // Add your own tests here

    printf("All tests passed. You are awesome!\n");

    return 0;
}

Your rotate_thirteen function will be called directly in marking. The main function is only to let you test your rotate_thirteen function

You can add more assert tests to main to test your rotate_thirteen function.

You will need to use your strings_equal function from the strings_equal activity for tests to work.

You may want to use your rotate_one function from the rotate_one activity.

You may want to use your rotate_thirteen function from the rotate_thirteen activity.

When you think your program is working you can use autotest to run some simple automated tests:

1091 autotest rotate_thirteen