C Array and String Practice Problems
Arrays and strings are the two things people get wrong most often in C, so we collected them here. In 81 problems you learn to stay inside an array's bounds and to handle strings that end with '\0'.
1. Filling and reading arrays #1–15
The indexes of int a[100]; run from 0 to 99.
If you read or write a[100], nothing stops you and you touch other memory.
Make the array big enough for the largest count the input can give you, and if you need starting values, fill it with int a[100] = {0};.
Syntax refresher: C Arrays: Declare, Initialize, and Loop · C for Loop
- Largest number and its position8P0005
- Numbers of the students who retake8P0374
- Is it on the list?8P0059
- Score statistics8P0003
- Travel time on a one-way bus route8P0224
- Masking banned words in chat8P0194
- Stamping a grid8P0061
- Remove duplicates8P0035
- A tournament where winning tires you8P0180
- A frog on stepping stones8P0193
- What the recipes are missing8P0237
- Finding a melody fragment in a song8P0437
- Spiral fill8P0128
- Vertical survey histogram8P0150
- Shaping a list by passing rule functions8P0239
2. Reverse, swap, copy #16–30
To swap two elements you need one temporary variable.b = a does not copy an array, so move the values with a loop or memcpy.
Removing an element from the middle means shifting every element after it one place forward.
Syntax refresher: C Arrays: Declare, Initialize, and Loop
- Showing one page of a board8P0284
- Swapping places in pairs8P0312
- Remove every match8P0058
- Rotate a list right8P0032
- Edit only the copy8P0060
- Shortest to the front, tallest to the back8P0317
- Edit a playlist8P0110
- Best window average8P0106
- Folding a paper strip8P0354
- Save slots for a list8P0107
- A function that reverses ranges8P0062
- Who moved after the seat swaps8P0176
- Tidying ranges of a warehouse shelf8P0177
- A function that takes any number of judge scores8P0333
- Lists inside lists and shallow copies8P0406
3. Sorting and 2D arrays #31–36
To sort, write the nested loops yourself or use qsort from <stdlib.h>.int a[3][4] has 3 rows and 4 columns, and you write a[row][col] in that order.
Mixing up the row and the column is the most common mistake.
Syntax refresher: C Arrays: Declare, Initialize, and Loop
4. Reading strings and measuring length #37–49
A string is a char array with '\0' at the end, so the array has to be one element bigger than the number of characters.scanf("%s", s) stops at a space, so use fgets to read a whole line.
A character is also a number, so you can calculate with c - '0' and c - 'a'.
Syntax refresher: C Strings: char Arrays, strlen, strcpy, strcmp · C scanf String Input: %s and fgets
- Count the vowels8P0009
- First and last position of a character8P0053
- Letters picked along the diagonal8P0283
- Swap the case8P0025
- Characters left in a message with Korean text8P0341
- Letter numbers in a note8P0192
- Caesar cipher8P0026
- Stamp a case pattern8P0230
- The longest stretch typed with one hand8P0269
- Mark where two lines differ8P0143
- Normalize a web address path8P0144
- Title case with exception words8P0145
- From base a to base b8P0093
5. Comparing and searching #50–59
s1 == s2 compares addresses, not contents.
Compare contents with strcmp(s1, s2) == 0.
To find a character or a word, use strchr or strstr, or match it yourself with a loop.
Syntax refresher: C Strings: char Arrays, strlen, strcpy, strcmp
- A price tag with dots and commas swapped8P0412
- Find and replace all8P0055
- Count overlapping occurrences8P0030
- Compare two words in dictionary order8P0056
- Trim whitespace from each line8P0057
- Sales log totals8P0102
- Template filler8P0101
- Find next and find previous8P0356
- Case-insensitive sort8P0103
- Guide Words on Dictionary Pages8P0386
6. Build, slice, reverse #60–81
When you build a new string, you have to put '\0' at the end yourself.
If you don't, printf cannot find the end and prints garbage characters.
Printing to a fixed width is handled by formats such as %5d, %05d and %-5s.
Syntax refresher: C Strings: char Arrays, strlen, strcpy, strcmp · C printf(): Format Specifiers and Output
- Star triangle8P0007
- Count the words8P0012
- Palindrome check8P0004
- Reverse it, keep the sign8P0252
- Cut from the a-th to the b-th character8P0054
- Reverse the word order8P0028
- A note cipher that reverses each half8P0316
- Letter pyramid8P0097
- Right-align the names8P0029
- Run-length compression8P0027
- Digit ribbon pieces8P0095
- Decoding hex color codes8P0416
- Multi-month calendar8P0126
- Wrap to a width8P0104
- Read the settings after the address8P0236
- Mirror lettering for a window8P0174
- A horizontal bar report8P0175
- A table of contents with numbers and dot leaders8P0440
- Score table8P0096
- Finish a link by attaching search filters8P0290
- Checking a receipt with subtotals8P0268
- Narrowing down a PIN from worn keys8P0321
Good to know
for (i = 0; i < strlen(s); i++)measures the length again on every pass. Store the length in a variable once.- If you get a Segmentation fault, check the array size, the indexes, and the place for the
'\0'at the end of the string first.
See also
- C Pointer Practice Problems — 16 problems
- C Loop Practice Problems: for, while, do-while — 50 problems
- 100 C Programming Practice Problems for Beginners — 100 problems
- Learn C — lessons with explanations and examples
- C reference
- C error messages
- Other problem sets