#define MAXSTRLEN 255
#define TRUE 1
#define FALSE 0
typedef struct
{
char str[MAXSTRLEN];
int len;
}s_string;
int concat(s_string *s1, s_string *s2, s_string *t)
{
}
void main()
{
s_string *s,*t,*r;
clrscr();
s=(s_string *)malloc(sizeof(s_string));
t=(s_string *)malloc(sizeof(s_string));
r=(s_string *)malloc(sizeof(s_string));
gets(s->str);
gets(t->str);
s->len=strlen(s->str);
t->len=strlen(t->str);
if(!concat(s,t,r)) printf("the string is cut!\n");
else printf("the string is uncut.\n");
printf("%s\n",r->str);
}
用C连接两个字符串
答案:2 悬赏:50 手机版
解决时间 2021-01-25 09:10
- 提问者网友:心牵心
- 2021-01-25 00:19
最佳答案
- 五星知识达人网友:何以畏孤独
- 2021-01-25 01:13
好久不玩这个了:下面纯手工编写, 没有测试, 你可以测试一下看看。
int concat(s_string *s1, s_string *s2, s_string *t)
{
int totalLength = 0 ;
int temp = 0 ;
int i=0;
int retValue = 0;
totalLength = s1->len + s2->len
if totalLength > sizeof(t) {retValue = 1}
if s1->len > sizeof(t) -1 || s2->len > sizeof(t)-1 {retValu = 2; return retValue;}
for(i=0;ilen;i++)
{
t->str[i] = s1->str[i];
}
if retValue ==0
{
for (i=s1->len;ilen+s2->len;i++)
{
t->str[i]=s2->str[i-s1->len]
}
}
else
{
for (i=s1->len;i
{
t->str[i] = s2->str[i-s1->len]
}
}
t->str[i] = '\0'
return retValue;
}
int concat(s_string *s1, s_string *s2, s_string *t)
{
int totalLength = 0 ;
int temp = 0 ;
int i=0;
int retValue = 0;
totalLength = s1->len + s2->len
if totalLength > sizeof(t) {retValue = 1}
if s1->len > sizeof(t) -1 || s2->len > sizeof(t)-1 {retValu = 2; return retValue;}
for(i=0;i
{
t->str[i] = s1->str[i];
}
if retValue ==0
{
for (i=s1->len;i
{
t->str[i]=s2->str[i-s1->len]
}
}
else
{
for (i=s1->len;i
t->str[i] = s2->str[i-s1->len]
}
}
t->str[i] = '\0'
return retValue;
}
全部回答
- 1楼网友:北城痞子
- 2021-01-25 01:27
#include
#include
#include
int main(void)
{
char string1[20],string2[20],string3[40];
int len1,len2,i = 0,j=0;
printf("输入第一个字符串!:");
gets(string1);
printf("输入第二个字符串!:");
gets(string2);
len1 = strlen(string1);
len2 = strlen(string2);
string1[len1-1] = ‘\0’;
string1[len2-1] = ‘\0’;
while(string1[i]!='\0'&&string2[i]!='\0')
{
string3[j++] = string1[i];
string3[j++] = string2[i];
i++;
}
if(string1[i] == '\0')
{
while(string2[i]!='\0')
{
string3[j++] = string2[i];
i++;
}
}
if(string2[i] == '\0')
{
while(string1[i]!='\0')
{
string3[j++] = string1[i];
i++;
}
}
string3[i] = '\0';
printf("%s\n",string3);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯