CTF: Strings
11 challenges — Week 5
Strings Challenges
| 11 challenges | Week 5 | Week 5 (Arrays, Strings, and Command-Line Arguments) |
String manipulation exercises in C covering character arrays, string functions from
Difficulty Breakdown
| Level | Count |
|---|---|
| Beginner | 4 |
| Intermediate | 4 |
| Advanced | 3 |
Challenges
1. Caesar Cipher
| Difficulty: Intermediate | Time: ~20 min | Type: complete program |
Write a program that implements a Caesar cipher or rotation cipher, which shifts every letter forward by a given number. Letters wrap around (X+3=A). Non-letter characters remain unchanged. Output is uppercase.
Concepts: character-by-character string iteration with strlen(), character classification and conversion (isupper, tolower, toupper), modular arithmetic for wraparound (%, if-else for range checking), ASCII character arithmetic (incrementing/decrementing char), handling non-letter characters
2. Count Letters
| Difficulty: Beginner | Time: ~10 min | Type: complete program |
Write a program that prompts the user for text until the user types ‘quit’, then outputs the total number of characters typed (excluding the ‘quit’ command itself).
Concepts: while loop with sentinel value (quit), string comparison (strcmp), accumulator pattern (sum += …), strlen() for counting characters, user input with gets() or fgets()
3. Count Matches
| Difficulty: Beginner | Time: ~10 min | Type: function |
Write a function count_matches that accepts a search string (char), an array of strings (char[]), and the array size, and returns the count of strings containing the search string as a substring. An empty search string is considered a substring of any string.
Concepts: arrays of strings (char*[] and 2D char arrays), strstr() for substring search, pointer arithmetic and NULL pointer handling, loop through array with size parameter, counting/accumulator pattern
4. Count Occurrences
| Difficulty: Intermediate | Time: ~15 min | Type: function |
Write a function count_occurrences that accepts a search substring and a target string, and returns the number of non-overlapping locations where the search substring is found. If either string is NULL or empty, return 0.
Concepts: NULL pointer checking, empty string handling, substring search using strncmp(), for loop with manual index advancement, non-overlapping vs overlapping occurrences
5. Is Suffix
| Difficulty: Intermediate | Time: ~12 min | Type: function |
Write is_suffix(char* s1, char* s2) that returns 1 if s1 ends with all characters of s2 in order (i.e., s2 is a suffix of s1), and 0 otherwise. An empty s2 should return 1 (empty string is suffix of all strings).
Concepts: strstr() for substring search, pointer arithmetic and pointer comparison, strlen() and string length calculation, suffix vs substring (suffix must end the string), empty string edge cases
6. Is Vowel
| Difficulty: Beginner | Time: ~8 min | Type: function |
Write is_vowel(char* s) that returns 1 if the string is a single vowel character (a, e, i, o, u, case-insensitive), and 0 otherwise. Returns 0 for empty strings, multiple characters, consonants, and digits.
| Concepts: strlen() for length check, single character extraction (s[0]), character classification and case conversion (tolower), character comparison with literals (‘a’, ‘e’, etc.), boolean logic (OR with | ) |
7. Locate Overlap
| Difficulty: Advanced | Time: ~25 min | Type: function |
Write locate_overlap(char* left_frag, char* right_frag, int* offset) that finds the maximal overlap when left_frag is followed by right_frag. Overlap can be at the boundary (e.g., ‘hello’ and ‘lol’ overlap by 2 at the end/beginning). Returns the overlap amount. Sets *offset (via pointer) to the starting position of the overlap within left_frag.
Concepts: pointer parameters (int* offset), dereferencing pointers (*offset = value), string length and boundary logic, strstr() for substring search, strncmp() for length-limited comparison
8. Match At Offset
| Difficulty: Advanced | Time: ~20 min | Type: function |
Write match_at_offset(char* left_frag, char* right_frag, int offset) that tests if right_frag matches left_frag starting at the given offset, with right_frag extending past the end of left_frag. Returns true (1) only if the offset is valid (>0, <length of left_frag) AND right_frag extends past the end of left_frag AND the overlapping characters match.
Concepts: pointer arithmetic (left_frag + offset), strncmp() for length-limited comparison, strlen() and boundary checking, boolean return values (0/1 or bool), offset validation (>0, <length)
9. Overlap
| Difficulty: Advanced | Time: ~25 min | Type: function |
Write overlap(char* s1, char* s2) that returns the number of characters of overlap between two strings. Overlap can occur at either end: suffix of s1 matching prefix of s2, OR suffix of s2 matching prefix of s1, OR s1 completely contained in s2, OR s2 completely contained in s1. Return the number of overlapping characters for the best match found.
Concepts: strstr() and strncmp() for substring/overlap detection, checking multiple directions (s1→s2 and s2→s1), suffix-prefix matching, substring containment checking, maximal matching (finding the largest overlap)
10. Print Letters
| Difficulty: Beginner | Time: ~8 min | Type: function |
Write print_letters(char* word) that prints each character of the string separated by commas. The first character has no comma before it. Output ends with a newline. Format: ‘A, t, m, o, s, p, h, e, r, e’
Concepts: fencepost problem (first element special, no preceding comma), for loop with length-dependent iteration, strlen() for string length, printf with %c for character output, string indexing (word[i])
11. Repeat
| Difficulty: Intermediate | Time: ~12 min | Type: function |
Write repeat(char* s, int n) that returns a heap-allocated string containing the input string s concatenated n times. If n <= 0, return an empty string (allocated). Must properly allocate memory with malloc/calloc and ensure the result is null-terminated. Caller must free the returned pointer.
Concepts: dynamic memory allocation (malloc/calloc), calculating required buffer size, string copying with loops or strcpy/strcat, null terminator placement, pointer return values