C语言编程>第二十周 ③ 请补充fun函数,该函数的功能是:把字符串s中的字符按字符的ASCII码升序排列,处理后的字符串仍然保存在原串中,字符串及其长度作为函数参数传入。

例题:请补充fun函数,该函数的功能是:把字符串s中的字符按字符的ASCII码升序排列,处理后的字符串仍然保存在原串中,字符串及其长度作为函数参数传入。

例如,若干输入 “edcba”,则输出为 “abcde”。
请勿改动主函数main与其它函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。

代码如下:

#include<stdio.h>
#define N 100
void fun(char p[],int n)
{
    
    
	int i,j;
	char t;
	for(i=0;i<n;i++)
		for(j=i;j<n;j++)
			if(p[i]>p[j])
			{
    
    
				t=p[j];
				p[j]=p[i];
				p[i]=t;
			}
}
main()
{
    
    
	int i=0,strlen=0;
	char s[N];
	printf("Please intput a string:\n");
	gets(s);
	while(s[i]!='\0')
	{
    
    
		strlen++;
		i++;
	}
	fun(s,strlen);
	printf("Display string:\n");
	puts(s);
}

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

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

猜你喜欢

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