zcmu-4921 字符串连接

Description

不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。

Input

每一行包括两个字符串,长度不超过100。

Output

可能有多组测试数据,对于每组数据,
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输出连接后的字符串。

Sample Input

abc def

Sample Output

abcdef

刚开始看到无冗余,用了动态分配内存,结果超时,然后换了下面这个。

参考博客链接

【通过代码】

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

void contact(char *str, const char *str1, const char *str2)
{
    int i, j;

    for(i = 0; str1[i] != '\0'; i ++)
    {
        str[i] = str1[i];
    }
    for(j = 0; str2[j] != '\0'; j ++)
    {
        str[i + j] = str2[j];
    }
    str[i + j] ='\0';
}

int main()
{
    char str[201];
    char str1[101];
    char str2[101];

    while(scanf("%s%s",str1,str2) != EOF)
    {
        contact(str, str1, str2);
        printf("%s\n",str);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/82081784
今日推荐