字符串连接(c语言实现)
起因今天九度刷题的时候,发现一个不调用任何c的库函数实现字符串拼接的代码很多人写的过于复杂,链表都用上了,真的不至于,只要知道字符串的最后截止符是'\0'.
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
每一行包括两个字符串,长度不超过100。
可能有多组测试数据,对于每组数据,
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输出连接后的字符串。
abc def
abcdef
#include <stdio.h>#include <stdlib.h>void contact(char *str, const char *str1, const char *str2);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';}