gpt4 book ai didi

c - C语言如何交替显示两个字符串字符

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:38 25 4
gpt4 key购买 nike

例如:s1 = "ABC"

s2 = "qwerty"

s3 将是“AqBwCerty”

全部为大写字母

它将是“AQBWCERTY”

如何创建?

谢谢~

这是我当前在主函数中的代码:

char w[100];
char s[50] = "abcderf";
char t[50] = "ARTYY";
int len = strlen(s);
int len1 = strlen(t);
int i, j;
if (len > len1) {
t[50] + s[50];
}
printf("%s", w);

最佳答案

像这样尝试:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char* interleaveStr(char* s1, char* s2) {
char* space = malloc(strlen(s1) + strlen(s2) + 1);
char* newStr = space;
int turn = 0;
while (*s1 && *s2) {
*newStr++ = !turn ? *s1++ : *s2++;
turn = !turn;
}

while (*s1) {
*newStr++ = *s1++;
}
while (*s2) {
*newStr++ = *s2++;
}
*newStr = '\0';
return space;
}

int main() {
char* s1 = "ABC";
char* s2 = "qwerty";
char* inter = interleaveStr(s1, s2);
printf("%s\n", inter);
free(inter);
}

关于c - C语言如何交替显示两个字符串字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50312376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com