תרגול: מעבר על מערכים ומחרוזות
שאלות 31 עד 46 של יחידה 8: מחרוזות בזיכרון, דפוסי מעבר, תוכניות מחרוזות עם fgets ו־string.h, מיון תווים (ניסוי מעבדה 16), פונקציית חיפוש ומיון בועות ראשון. כל תשובה מוסתרת עד שפותחים אותה.
השיעור הזה כתוב באנגלית בלבד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
32. Capacity includes the terminator
Find and repair the fault. The array must store the complete C string.
char name[4] = "NODE";נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
33. Reading a line
A device label may contain spaces. Explain why fgets(label, sizeof label, stdin) fits better than scanf("%s", label).
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
36. Backward traversal
Read eight integers. Print the array from the last valid index down to index 0, on one line.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
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.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.