C language programming> thirteenth week ③ Write a function fun, its function is: to realize the connection of two characters (do not use the library function strcat), that is, connect the string pointed to by str2 to the string pointed to by str1 .

Example: Write a function fun. Its function is to realize the connection of two strings (without using the library function strcat), that is, connect the string pointed to by str2 to the string pointed to by str1.

例如,分别输入下面两个字符串:
just--
test-
则程序输出:just--test-
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<stdio.h>
#include<conio.h>
void fun(char str1[],char str2[])
{
    
    
	int i=0,n=0;
	char*p=str1,*q=str2;
	while(*p)
	{
    
    
		p++;
		n++;
	}
	i=n;
	while(*q)
	{
    
    
		str1[i]=*q;
		q++;
		i++;
	}
	str1[i]='\0';
}
main()
{
    
    
	char s1[100],s2[50];
	FILE*out;
	printf("Enter s1 and s2:\n");
	scanf("%s%s",s1,s2);
	printf("s1=%s\n",s1);
	printf("s2=%s\n",s2);
	fun(s1,s2);
	printf("After invoking:\n");
	printf("%s\n",s1);
	out=fopen("outfile.dat","w");
	strcpy(s1,"This");
	fun(s1,"is ");
	fun(s1,"a ");
	fun(s1,"test");
	fun(s1,"string.");
	fprintf(out,"%s\n",s1);
	fclose(out);
}

The output running window is as follows:
Insert picture description here

越努力越幸运!
加油,奥力给!!!

Guess you like

Origin blog.csdn.net/qq_45385706/article/details/111994245