C语言问题 求解
2020-05-02
题目的意思是说s2比s1短,或长度相等,就把整个s2接到s1后面吗?
#include <stdio.h>
#include <string.h>
int main(void) {
char s1[27], s2[27];
char *p, *t, s[53];
fgets(s1, 27, stdin);
fgets(s2, 27, stdin);
t = strchr(s1, '\n');
if (t) *t = '\0';
t = strchr(s2, '\n');
if (t) *t = '\0';
int l1 = strlen(s1);
int l2 = strlen(s2);
if (l1 < l2) {
p = s1;
t = s;
while (*p) *t++ = *p++;
p = s2 + (p - s1);
while (*p) *t++ = *p++;
}
else {
p = s1;
t = s;
while (*p) *t++ = *p++;
p = s2;
while (*p) *t++ = *p++;
}
puts(s);
return 0;
}
char s2[27];
char s[26+27];
scanf("%s",s1);scanf("%s",s2);
char* p=s1;
char *sp=s;
st:if(*p){*sp=*p;sp++;p++;goto st;}
p=s2;
st2:if(*p){*sp=*p;sp++;p++;goto st2;}
*sp=0;
printf("%s",s);
能不能写完整的程序。
#include <stdio.h>
int main()
{
char s1[27];
char s2[27];
char s[26 + 27];
scanf("%s", s1);
scanf("%s", s2);
char* p = s1;
char* p2 = s2;
char* sp = s;
if (strlen(p) > strlen(p2)) {
p = s2;
p2 = s1;
}
st:if (*p) { *sp = *p; sp++; p++; goto st; }
p = p2;
st2:if (*p) { *sp = *p; sp++; p++; goto st2; }
*sp = 0;
printf("%s", s);
return 0;
}
之前用手机写的……