C语言编程>第十二周 ② 请编写函数fun,其功能是:将str所指字符串中除了小标为奇数、ASCII值也为奇数的字符之外,其余的所有字符都删除,串中剩余字符所形成的一个新串放在s所指的数组中。

例题:请编写函数fun,该函数的功能是:移动字符串中的内容,移请编写函数fun,其功能是:将str所指字符串中除了小标为奇数、ASCII值也为奇数的字符之外,其余的所有字符都删除,串中剩余字符所形成的一个新串放在s所指的数组中。

例如,若str所指字符串中的内容为ABCDEFG12345,其中字符A的ASCII码值虽为奇数,但所在元素的下标为偶数,因此必须删除;而字符1的ASCII码值为奇数,所在数组中的下标也为奇数,因此不应当删除,其他以此类推。最后s所指的数组中的内容应是135。
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<conio.h>
#include<stdio.h>
#include<string.h>
void fun(char*str,char s[])
{
    
    
	int i,j=0,n;
	n=strlen(str);
	for(i=0;i<n;i++)
	if(i%2!=0&&str[i]%2!=0)
	{
    
    
		s[j]=str[i];
		j++;
	}
	s[j]='\0';
}
main()
{
    
    
	char str[100],s[100];
	FILE*out;
	printf("\nPlease enter string:");
	scanf("%s",str);
	fun(str,s);
	printf("\nThe result is:%s\n",s);
	out=fopen("outfile.dat","w");
	strcpy(str,"Please enter string:");
	fun(str,s);
	fprintf(out,"%s",s);
	fclose(out);
}

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

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

猜你喜欢

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