Practice: traversal and strings
Questions 31 to 46 of unit 8: strings in memory, traversal patterns (forward, backward, a range, a step, neighbours), string programs with fgets and string.h, character classification (lab experiment 16), a search function and a first bubble sort. Every answer is hidden until you open it.
How to use this page
This page continues Practice: arrays and keeps its numbering. Questions 31 to 34 are short checks; questions 35 to 46 are complete programs. For each program, submit working code and three tests: a normal case, a boundary case, and a case that exposes an index or capacity mistake.
The string programs read whole lines with fgets, so a label may contain spaces. A line of at most 30 characters needs an array of 32: 30 characters, the newline, and '\0'.
Strings in memory
31. String memory
For char word[] = "CODE";, write the value stored at each index from 0 to 4. Name the special value at index 4.
Try it yourself first, then open the solution step by step.
32. Capacity includes the terminator
Find and repair the fault. The array must store the complete C string.
char name[4] = "NODE";Try it yourself first, then open the solution step by step.
33. Reading a line
A device label may contain spaces. Explain why fgets(label, sizeof label, stdin) fits better than scanf("%s", label).
Try it yourself first, then open the solution step by step.
34. String library map
Match each function to its job: strlen, strcmp, strcpy, strcat. Jobs: find the length, compare two strings, copy a string, append a string.
Try it yourself first, then open the solution step by step.
Traversal patterns
35. Forward traversal with indexes
Read eight integers. Visit every position from first to last and print one index:value pair per line.
Try it yourself first, then open the solution step by step.
36. Backward traversal
Read eight integers. Print the array from the last valid index down to index 0, on one line.
Try it yourself first, then open the solution step by step.
37. Traverse a selected range
Read ten integers and two indexes, start and end. Print the values from start to end, both included. Print INVALID unless 0 <= start <= end < 10.
Try it yourself first, then open the solution step by step.
38. Traverse with a step
Read ten integers. Print only the values at even indexes (0, 2, 4, 6, 8) by changing the loop step.
Try it yourself first, then open the solution step by step.
39. Adjacent-pair traversal
Read eight integers. For each index from 1 to 7, print the change from the previous element, data[i] - data[i - 1], all on one line.
Try it yourself first, then open the solution step by step.
String programs
40. Manual string length
Read one line of at most 30 characters with fgets. Count its characters by walking the string until '\0', without calling strlen. Do not count the newline.
Try it yourself first, then open the solution step by step.
41. Character classification laboratory (lab experiment 16)
Read one line of at most 30 characters. Count uppercase letters, lowercase letters, digits, and every other character. Do not count the newline. Print four labeled counts: UPPER, LOWER, DIGIT, OTHER.
Try it yourself first, then open the solution step by step.
42. Compare two strings
Read two lines of at most 20 characters. Remove each newline and use strcmp to print SAME when the strings match exactly, otherwise DIFFERENT.
Try it yourself first, then open the solution step by step.
43. Safe copy and append
Read two lines of at most 100 characters each. If both strings plus one separating space fit in a 64-element destination, join them with strcpy and strcat and print the result. Otherwise print TOO LONG.
Try it yourself first, then open the solution step by step.
44. Palindrome by two indexes
Read one line of at most 30 characters. Compare the first and the last character, then move both indexes inward. Print PALINDROME or NOT PALINDROME. The comparison is case-sensitive and includes spaces.
Try it yourself first, then open the solution step by step.
Search and sorting
45. Linear search function
Read ten integers and a target. Write a function find_first with an array parameter that returns the first index of the target, or -1 when it is absent. Return as soon as the first match is found.
Try it yourself first, then open the solution step by step.
46. Bubble sort introduction
Read eight integers. Sort them in nondecreasing order with bubble sort and print the sorted array. Track whether a pass made a swap, so an already sorted array can stop early.
Try it yourself first, then open the solution step by step.