编写实现两个字符串交替写入及迭代首尾改写的C程序问询
Solution: C Program for Alternating String Replacement from Both Ends
Here's a complete C program that meets your requirement of alternatingly writing two strings into an initial "X-filled" buffer, replacing characters from the start and end in each iteration until the entire buffer is rewritten. I’ll break down how it works step by step.
Full Code
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { // Define your two input strings (modify these as needed) const char *str1 = "I like "; const char *str2 = "programming!"; // Calculate total buffer length using strlen() as required int len1 = strlen(str1); int len2 = strlen(str2); int total_length = len1 + len2; // Allocate buffer and initialize with 'X's char *buffer = (char *)malloc(total_length + 1); if (buffer == NULL) { perror("Failed to allocate memory"); return 1; } memset(buffer, 'X', total_length); buffer[total_length] = '\0'; // Null-terminate the string int left_ptr = 0; // Pointer starting at the buffer's start int right_ptr = total_length - 1; // Pointer starting at the buffer's end int idx1 = 0; // Traverse str1 left-to-right int idx2 = len2 - 1; // Traverse str2 right-to-left printf("Initial buffer: %s\n", buffer); // Alternate replacing 'X's from both ends while (left_ptr <= right_ptr && (idx1 < len1 || idx2 >= 0)) { // Replace leftmost 'X' with next character from str1 if (idx1 < len1) { buffer[left_ptr] = str1[idx1]; left_ptr++; idx1++; printf("After left replacement: %s\n", buffer); } // Replace rightmost 'X' with next character from str2 (if space remains) if (idx2 >= 0 && left_ptr <= right_ptr) { buffer[right_ptr] = str2[idx2]; right_ptr--; idx2--; printf("After right replacement: %s\n", buffer); } } // Show the final complete string printf("\nFinal completed string: %s\n", buffer); // Clean up allocated memory free(buffer); return 0; }
Key Details & Explanation
- Buffer Setup: We calculate the total required buffer length by adding the lengths of both input strings (using
strlen()as specified). The buffer is dynamically allocated to work with any string lengths, then filled entirely with 'X's usingmemset(). - Dual Traversal: We use two pointers to track where to replace 'X's—one starting at the left (for
str1) and one at the right (forstr2). We traversestr1from start to end, andstr2from end to start to match your alternating rewrite requirement. - Iterative Replacement: In each loop iteration, we first replace the leftmost available 'X' with a character from
str1, print the buffer, then replace the rightmost available 'X' with a character fromstr2, and print again. This continues until all characters are written or the pointers meet in the middle. - Memory Safety: We check for allocation failures and free the buffer at the end to avoid memory leaks.
Sample Output
Running this program will produce output matching your example’s pattern:
Initial buffer: XXXXXXXXXXXXXXXXXXX After left replacement: IXXXXXXXXXXXXXXXXXX After right replacement: IXXXXXXXXXXXXXXXX! After left replacement: I XXXXXXXXXXXXXXXX! After right replacement: I XXXXXXXXXXXXXXXg! After left replacement: I lXXXXXXXXXXXXXXg! After right replacement: I lXXXXXXXXXXXXXng! ... Final completed string: I like programming!
内容的提问来源于stack exchange,提问作者user8310120
相关产品推荐
相关产品推荐

