C语言编程>第十七周 ④ 下列给定程序中,函数fun的功能是:在字符串的最前端加入n个“*”号,形成新串,并且覆盖原串。

例题:下列给定程序中,函数fun的功能是:在字符串的最前端加入n个“*”号,形成新串,并且覆盖原串。

例如:输入的字符串为“asdfg”,n为8,则输出为********asdfg
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。字符串的长度最长允许为79。

代码如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
void fun(char str[],int n)
{
    
    
	char a[80],*p;
	int i;
	p=str;
	for(i=0;i<n;i++)
		a[i]='*';
	do
	{
    
    
		a[i]=*p;
		i++;
		p++;
	}while(*p);
	a[i]='\0';
	strcpy(str,a);
}
main()
{
    
    
	int n;
	char str[80];
	printf("\nEnter a string:");
	gets(str);
	printf("\nThe string %s\n",str);
	printf("\nEnter n(number of*):");
	scanf("%d",&n);
	fun(str,n);
	printf("\nThe string after inster:%s\n",str);
}

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

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

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/112342221