字符串连接(c语言实现)

来源:岁月联盟 编辑:exp 时间:2012-10-13

 起因
今天九度刷题的时候,发现一个不调用任何c的库函数实现字符串拼接的代码很多人写的过于复杂,链表都用上了,真的不至于,只要知道字符串的最后截止符是'/0'.

题目描述:
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输入:
每一行包括两个字符串,长度不超过100。
输出:
可能有多组测试数据,对于每组数据,
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输出连接后的字符串。
样例输入:
abc def
样例输出:
abcdef

直接上我的AC代码吧
[cpp]
#include <stdio.h> 
#include <stdlib.h> 
 
void contact(char *str, const char *str1, const char *str2); 
  www.2cto.com
int main() 

    char str[201], str1[101], str2[101]; 
 
    while(scanf("%s%s",str1,str2) != EOF) 
    { 
        contact(str, str1, str2); 
        printf("%s/n",str); 
    } 
    return 0; 

 
/**
 * Description:字符串连接函数
 */ 
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'; 

图片内容