标签:
自己编写一个字符串链接函数:函数首部定义为void fun(char a[],char b[])其功能是:将形参数组b中的字符顺序连接到形参数组a的字符串后面。注:不能使用字符串的复制和连接函数。例如:a中的字符串为hello,b中的字符串为123,则字符串a中的内容为hello123。
main函数中输入两字符串赋给字符数组str1和str2,调用fun函数,实现两字符串的连接,并将连接好后的字符串输出。
#include<stdio.h>
#include<string.h>
int main()
{
void fun (char a[],char b[]);
char str1[100],str2[100];
gets(str1);
gets(str2);
fun(str1,str2);
return 0;
}
void fun (char a[],char b[])
{
int num1=0,num2=0,i;
num1=strlen(a);
num2=strlen(b);
for(i=0;i<=num2;i++)/*i<=num2而非i<num2,因为数组b中的\0没有赋值到数组a中,数组a没有输出结束符号,导致乱码的输出*/
{
a[num1++]=b[i];
}
printf("%s",a);
}
标签:
原文地址:http://www.cnblogs.com/boycelee/p/4564976.html