填空题 C语言
题号 #2256981

请补充函数proc(),该函数的功能是:把一个字符串中的字符(字母)按从小到大排序,并把这个全部由字母组成的字符串保存在原串中,函数返回这个字符串的长度。例如,若输入“abc12def”,则新字符串为abcdef,字符串长度为6。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填人所编写的若干表达式或语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#define M 20
int proc(char*str)
{
int i=0,j=0,k=0,m=0;
char t;
char*p—str;
while(*p)
{
if((*p>='A'&&*p<='Z')||(*p>='a
&&*p<='2'))
【1】 ;
p++;
}
*(str+i)='\0';
【2】 ;
while(*(p+j))
{
k=j;
【3】 ;
while(*(p+k))
{
if(*(p+k)<*(str+m))
{
t=*(str+m);
*(str+m)=*(p+k);
*(p+k)=t;
}
k++;
}
j++;
}
return i;
}
void main()
{
char str[81];
int n;
system("CLS");
printf("Input the original string");
gets(str);
printf("***The Original string***\n");
puts(str);
printf("***The new string***\n");
n=proc(str):
puts(str);
printf("***The length of new string is:
%d***\n",n):
}

进入在线模考

参考答案

暂无

答案解析

【1】*(str+i++)=*p【2】p=str【3】m=j
【解析】题目中要求将一个字符串的字母字符按从小到大排序。首先要将字符串中的所有字母字符选择出来放在原来的字符串中。因此,【1】处填“*(str+i++)=*P”。由后面的代码可知,变量p指向新的字符串str,因此其初始值为字符串str的首地址。因此,【2】处填“p=str”。然后将得到的新的字符串排序,因此,【3】处填“m=j”。

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考