{ }codelper

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'.

81 problemsLevel 1–3Solve in C

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

  1. Largest number and its position8P0005
  2. Numbers of the students who retake8P0374
  3. Is it on the list?8P0059
  4. Score statistics8P0003
  5. Travel time on a one-way bus route8P0224
  6. Masking banned words in chat8P0194
  7. Stamping a grid8P0061
  8. Remove duplicates8P0035
  9. A tournament where winning tires you8P0180
  10. A frog on stepping stones8P0193
  11. What the recipes are missing8P0237
  12. Finding a melody fragment in a song8P0437
  13. Spiral fill8P0128
  14. Vertical survey histogram8P0150
  15. 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

  1. Showing one page of a board8P0284
  2. Swapping places in pairs8P0312
  3. Remove every match8P0058
  4. Rotate a list right8P0032
  5. Edit only the copy8P0060
  6. Shortest to the front, tallest to the back8P0317
  7. Edit a playlist8P0110
  8. Best window average8P0106
  9. Folding a paper strip8P0354
  10. Save slots for a list8P0107
  11. A function that reverses ranges8P0062
  12. Who moved after the seat swaps8P0176
  13. Tidying ranges of a warehouse shelf8P0177
  14. A function that takes any number of judge scores8P0333
  15. 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

  1. Sort the numbers8P0036
  2. Sorting sign letters8P0253
  3. Row and column sums8P0033
  4. Number order and dictionary order8P0320
  5. Table of shared free days8P0234
  6. Union, intersection, difference8P0099

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

  1. Count the vowels8P0009
  2. First and last position of a character8P0053
  3. Letters picked along the diagonal8P0283
  4. Swap the case8P0025
  5. Characters left in a message with Korean text8P0341
  6. Letter numbers in a note8P0192
  7. Caesar cipher8P0026
  8. Stamp a case pattern8P0230
  9. The longest stretch typed with one hand8P0269
  10. Mark where two lines differ8P0143
  11. Normalize a web address path8P0144
  12. Title case with exception words8P0145
  13. 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

  1. A price tag with dots and commas swapped8P0412
  2. Find and replace all8P0055
  3. Count overlapping occurrences8P0030
  4. Compare two words in dictionary order8P0056
  5. Trim whitespace from each line8P0057
  6. Sales log totals8P0102
  7. Template filler8P0101
  8. Find next and find previous8P0356
  9. Case-insensitive sort8P0103
  10. 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

  1. Star triangle8P0007
  2. Count the words8P0012
  3. Palindrome check8P0004
  4. Reverse it, keep the sign8P0252
  5. Cut from the a-th to the b-th character8P0054
  6. Reverse the word order8P0028
  7. A note cipher that reverses each half8P0316
  8. Letter pyramid8P0097
  9. Right-align the names8P0029
  10. Run-length compression8P0027
  11. Digit ribbon pieces8P0095
  12. Decoding hex color codes8P0416
  13. Multi-month calendar8P0126
  14. Wrap to a width8P0104
  15. Read the settings after the address8P0236
  16. Mirror lettering for a window8P0174
  17. A horizontal bar report8P0175
  18. A table of contents with numbers and dot leaders8P0440
  19. Score table8P0096
  20. Finish a link by attaching search filters8P0290
  21. Checking a receipt with subtotals8P0268
  22. Narrowing down a PIN from worn keys8P0321

Good to know

See also