Programming Fundamentals
Information
- This page contains additional revision exercises for week 07.
- These exercises are not compulsory, nor do they provide any marks in the course.
- You cannot submit any of these exercises, however autotests may be available for some them (Command included at bottom of each exercise if applicable).
Exercise — individual:
Secret Code
TOP SECRET // COMP1511 ONLY
Write a file secret_code.c which allows you to scan in messages encrypted with Tom's Secret Code,
and then print them out (ending in a newline).
Tom's Secret Code works two letters at a time. Given some ciphertext (text that has been encrypted with Tom's Secret Code), take the first two letters. The first letter of the plaintext (unencrypted text) is the letter with the smaller ascii value of those two encrypted letters. For example, if the first two letters of ciphertext were "GD", the first letter of the plaintext would be "D".
To explain the code, the following diagram demonstrates how the code "CZuOMUPP1i5fg112" is decrypted as "COMP1511". In each pair of letters, the one with the lower ascii value was the one that was part of the plaintext.
| Cipher Text | C | Z | u | O | M | U | P | P | 1 | i | 5 | f | g | 1 | 1 | 2 |
| ASCII Values | 67 | 90 | 117 | 79 | 77 | 85 | 80 | 80 | 49 | 105 | 53 | 102 | 103 | 49 | 49 | 50 |
| Correct Answer | C | O | M | P | 1 | 5 | 1 | 1 | ||||||||
Your program should behave exactly as these examples do:
dcc secret_code.c -o secret_code ./secret_code abbccddeeffggh abcdefg ./secret_code CZuOMUPP1i5fg112 COMP1511
Assumptions/Restrictions/Clarifications
- You should not assume that there will be an even number of inputs. If there is an odd number of characters, you should ignore the last character.
- You could be given any printable ascii character as input (lowercase letters, uppercase letters, newlines, symbols, etc.)
- Your program should always print a newline at the end of it's output
- This exercise is very difficult to solve using
fgets. You should solve this usingscanf("%c", ...)
Exercise — individual:
Strings Equal
Download strings_equal.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity strings_equal
You've probably noticed that
you can't use the comparison operator, ==,
to compare strings.
So, how do we compare two strings?
For this activity,
you'll be writing a function to do exactly that:
string.h in this exercise.
It takes two strings, string1 and string2,
and, if they are element-for-element the same,
it returns 1, and 0 otherwise.
You shouldn't ever read beyond
the null-terminator of either string.
Download strings_equal.c here, or copy it to your CSE account using the following command:
cp -n /import/adams/A/cs1511/public_html/26T3/activities/strings_equal/strings_equal.c .
Your task is to add code to this function in strings_equal.c:
// Takes two strings, and if they are the same,
// returns 1, or 0 otherwise.
int strings_equal(char *string1, char *string2) {
// Your code goes here!
// Don't forget to return your result.
return 0;
}
strings_equal.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 will terminate program if evaluated as false
assert(strings_equal("", "") == 1);
assert(strings_equal(" ", "") == 0);
assert(strings_equal("", " ") == 0);
assert(strings_equal(" ", " ") == 1);
assert(strings_equal("\n", "\n") == 1);
assert(strings_equal("This is 17 bytes.", "") == 0);
assert(strings_equal("", "This is 17 bytes.") == 0);
assert(strings_equal("This is 17 bytes.", "This is 17 bytes.") == 1);
assert(strings_equal("Here are 18 bytes!", "This is 17 bytes.") == 0);
printf("All tests passed. You are awesome!\n");
return 0;
}
Your strings_equal function will be called directly in marking. The main function is only to let you test your strings_equal function
You can add more assert tests to main to test your strings_equal function.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest strings_equal
Exercise — individual:
String to Upper
Download string_to_upper.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity 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
string_to_upper.c also contains a simple main function to help you test your solution.
int main(int argc, char *argv[]) {
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[]) {
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:
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest string_to_upper
Exercise — individual:
String to Lower
Download string_to_lower.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity string_to_lower
For this activity,
you'll be writing the function string_to_lower.
It takes a string and converts it to lower case.
string_to_lower.c also contains a simple main function
to help you test your solution.
int main(void) {
char str[MAX_LEN] = "Hi, mY nAmE iS sPonGEbOb sQuArePanTS.";
string_to_lower(str);
printf("%s\n", str);
return 0;
}
Your string_to_lower function will be called directly in marking. The main function is only to let you test your string_to_lower function.
Here is how string_to_lower.c should behave after you add the correct code to the function string_to_lower:
dcc string_to_lower.c -o string_to_lower ./string_to_lower hi, my name is spongebob squarepants.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest string_to_lower
Exercise — individual:
String Manipulation
Download string_manipulation.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity string_manipulation
Your task is to add code to this function in string_manipulation.c:
// TODO: move code sections into functions
}
Implement the different functions listed in the string_manipulation.c file.
No autotests are provided for this question.
Examples
dcc struct_tutorial.c -o struct_tutorial ./struct_tutorial Hello, World! Goodbye Goodbye is before World.
Exercise — individual:
String Length
Download string_length.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity 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.
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 will terminate program if evaluated as false
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:
1511 autotest string_length
Exercise — individual:
String Copy
Download string_copy.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity 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.
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:
1511 autotest string_copy
Exercise — individual:
String Reverse
Download string_reverse.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity string_reverse
For this activity,
you'll be writing the function string_reverse.
It takes a string and reverses it in place.
string_reverse.c also contains a simple main function to help you test your function string_reverse
int main(int argc, char *argv[]) {
char str[] = ".'neetneves' :egassem terces A";
string_reverse(str);
printf("%s\n", str);
return 0;
}
Your string_reverse 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 string_reverse.c -o string_reverse ./string_reverse A secret message: 'seventeen'.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest string_reverse
Exercise — individual:
String Manipulation
Download string_manipulation.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity string_manipulation
Your task is to add code to this function in string_manipulation.c:
// TODO: move code sections into functions
}
Implement the different functions listed in the string_manipulation.c file.
No autotests are provided for this question.
Examples
dcc struct_tutorial.c -o struct_tutorial ./struct_tutorial Hello, World! Goodbye Goodbye is before World.
Exercise — individual:
String Search
Your job is to write a program called string_search.c which lets us count the
number of times we see any our of "search terms" in a list of words.
The search terms will be provided to our program via command line arguments. For example:
./string_search hello there
would run the program with 2 search terms - hello and there.
TASK 1: Edit the arguments to your main function so that it can take in
command line arguments (aka, the search terms). Try printing out the values of
argc and argv and changing the values you type after ./string_search to
see what they contain!
TASK 2: Scan in words from standard input until Ctrl-D is pressed.
TASK 3: Count the number of times the search terms appear in the input.
Examples
dcc string_search.c -o string_search ./string_search same Enter list of words: same sand same send shade same shadow There were 3 occurrence(s) in the input. ./string_search many search terms Enter list of words: terms many same many shade search research There were 4 occurrence(s) in the input.
Assumptions/Restrictions/Clarifications
- You can assume that each word will be no longer than 128 characters long
- You can assume that there will only be 1 word per line
- You can assume that the search terms will never have repeats in them
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest string_search