Lesson 21 of 23
paste
The paste Command
paste merges corresponding lines from two inputs side-by-side with a delimiter:
$ paste -d: names.txt ages.txt
alice:30
bob:25
carol:35
Line 1 of names.txt is joined to line 1 of ages.txt with :, and so on.
Your Implementation
Write void my_paste(const char *a, const char *b, char delim) that merges lines from a and b with delim.
The algorithm: advance through both strings simultaneously. For each pair of lines, print the line from a, then the delimiter, then the line from b, then a newline.
void my_paste(const char *a, const char *b, char delim) {
while (*a || *b) {
while (*a && *a != '\n') { putchar(*a); a++; }
putchar(delim);
while (*b && *b != '\n') { putchar(*b); b++; }
putchar('\n');
if (*a == '\n') a++;
if (*b == '\n') b++;
}
}
Two Pointers, One Loop
Unlike previous tools that operate on a single string, paste walks two strings at once. The while (*a || *b) condition continues as long as either string still has content — so if one is longer, its extra lines still appear (with an empty other side).
Your Task
Implement my_paste that merges lines from a and b with the given delimiter.
TCC compiler loading...
Loading...
Click "Run" to execute your code.