C语言程序从键盘上输入两个字符串若不相等将短的字符串连接到长的字符串的末尾并
答案:2 悬赏:80 手机版
解决时间 2021-03-08 09:59
- 提问者网友:浮克旳回音
- 2021-03-07 18:50
C语言程序从键盘上输入两个字符串若不相等将短的字符串连接到长的字符串的末尾并
最佳答案
- 五星知识达人网友:慢性怪人
- 2021-03-07 19:07
#include"stdio.h"
#include "string.h"
void main()
{
char s1[50];
char s2[50];
scanf("%s",s1);
scanf("%s",s2);
if(strcmp(s1,s2))
{
if(strlen(s1)>strlen(s1))
strcat(s1,s2);
else
strcat(s2,s1);
puts(s1);
}
else
puts("相同");
}
#include "string.h"
void main()
{
char s1[50];
char s2[50];
scanf("%s",s1);
scanf("%s",s2);
if(strcmp(s1,s2))
{
if(strlen(s1)>strlen(s1))
strcat(s1,s2);
else
strcat(s2,s1);
puts(s1);
}
else
puts("相同");
}
全部回答
- 1楼网友:野味小生
- 2021-03-07 19:26
#include
int main()
{
char a[100];
char b[100];
int i;
int j;
int len_a;
int len_b;
printf("input string a:\n");
gets(a);
printf("input string b:\n");
gets(b);
len_a = 0;
while(a[len_a])
{
len_a++;
}
len_b = 0;
while(b[len_b])
{
len_b++;
}
if(len_a>len_b)
{
i = len_a;
j = 0;
while(b[j])
{
a[i] = b[j];
i++;
j++;
}
a[i] = '\0';
printf("string a is longer than string b:\n");
printf("%s\n",a);
}
if(len_a {
i = 0;
j = len_b;
while(a[i])
{
b[j] = a[i];
i++;
j++;
}
b[j] = '\0';
printf("string b is longer than string a:\n");
printf("%s\n",b);
}
return 0;
}
运行效果:
C:\Documents and Settings\Owner\桌面>test.exe
input string a:
kjlasdjlafuiw3hj3k4
input string b:
56sfd6a
string a is longer than string b:
kjlasdjlafuiw3hj3k456sfd6a
C:\Documents and Settings\Owner\桌面>
int main()
{
char a[100];
char b[100];
int i;
int j;
int len_a;
int len_b;
printf("input string a:\n");
gets(a);
printf("input string b:\n");
gets(b);
len_a = 0;
while(a[len_a])
{
len_a++;
}
len_b = 0;
while(b[len_b])
{
len_b++;
}
if(len_a>len_b)
{
i = len_a;
j = 0;
while(b[j])
{
a[i] = b[j];
i++;
j++;
}
a[i] = '\0';
printf("string a is longer than string b:\n");
printf("%s\n",a);
}
if(len_a
i = 0;
j = len_b;
while(a[i])
{
b[j] = a[i];
i++;
j++;
}
b[j] = '\0';
printf("string b is longer than string a:\n");
printf("%s\n",b);
}
return 0;
}
运行效果:
C:\Documents and Settings\Owner\桌面>test.exe
input string a:
kjlasdjlafuiw3hj3k4
input string b:
56sfd6a
string a is longer than string b:
kjlasdjlafuiw3hj3k456sfd6a
C:\Documents and Settings\Owner\桌面>
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯