C语言编程>第十三周 ③ 编写一个函数fun,它的功能是:实现两个字符的连接(不使用库函数strcat),即把str2所指的字符串连接到str1所指的字符串后。

例题:编写一个函数fun,它的功能是:实现两个字符串的连接(不使用库函数strcat),即把str2所指的字符串连接到 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);
}

输出运行窗口如下:
在这里插入图片描述

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

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/111994245
今日推荐