编写程序将s1和s2中的字符串交叉复制到s3中
时间: 2024-05-03 17:20:56 浏览: 115
Sure, the program to interweave strings s1 and s2 into s3 can be written as follows:
```
#include <stdio.h>
#include <string.h>
int main(){
char s1[] = "string1";
char s2[] = "string2";
char s3[12]; // length of s1 + length of s2
int i = 0;
int j = 0;
int k = 0;
while (s1[i] != '\0' || s2[j] != '\0') {
if (s1[i] != '\0') {
s3[k++] = s1[i++];
}
if (s2[j] != '\0') {
s3[k++] = s2[j++];
}
}
s3[k] = '\0';
printf("Resulting string: %s\n", s3);
return 0;
}
```
This program creates 3 character arrays. It then iterates through s1 and s2, adding each character to s3 in turn until both s1 and s2 have been fully traversed, resulting in s3 containing an interweaving of the strings.
阅读全文