str为一个字符序列。请补充函数fun(),该函数的功能是:查找str中值为x的元素,返回该字符序列中值为x的元素个数,并把这些值为x的元素下标依次保存在数组bb中。例如,在“abcdefahij”中查找‘a’,结果为:2个‘a’,下标依次为 0、6。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
define N 20
int bb[N];
int fun(char *str,char ch)
{
int i=0, n=0;
char t=ch;
char *p=str;
while(*p)
{
if (【 】)
【 】;
p++;
i++;
}
return【 】;
}
main()
{
char str[N];
char ch;
int i, j,n;
clrscr();
printf("***Input the original string
***\n");
gets(str);
printf("***The Original ***\n");
puts(str);
printf("***Input character ***\n");
scanf("%c",&ch);
n=fun(str,ch);
printf("\nThe numbr of character is:
%d\n", n);
printf{"***The suffix of character
***\n");
for(i=0;i<n;i++)
printf("%d ",bb[i]);
}
第1题:
以下函数fun的功能是返回str所指字符串中以形参c中字符开头的后续字符串的首地址,例如,str所指字符串为Hello!,c中的字符为e,则函数返回字符串ello!的首地址。若str所指字符串为空或不包含c中的字符,则函数返回NULL,请填空。char *fun(char *str,char c){ int n=0; char *p=str; if(p!=NULL) while(p[n]!=c&&p[n]!=’\0’) n++; if(p[n]==’\0’) return NULL; return();}
第2题:
str为一个字符序列,序列由字符0和1组成。请补充函数 fun(),该函数的功能是:查找该字符序列中0字符连续出现的最长长度(即0字符的个数),如果有几个0字符串长度相同,只记录最后一个0字符串的相关信息。通过m和k返回最长0字符串的长度和最后0字符的下标。例如,如果输入“01001000”,结果为:0字符串最长长度为3,起始和结尾下标依次为5、7。
注意:部分源程序给出如下.
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
define N 80
void fun (【 】)
{
int i, j=0;
int bb[N];
char *p=str;
*m=0;
*k=0;
for(i=0;i<N;i++)
bb[i]=0;
i=0;
while(*(p+i))
{
if(*(p+i)='0')
{
【 】
i++;
}
else
{
j++;
i++;
}
if (【 】)
{
*m=bb[j];
*k=i-1;
}
}
}
main()
{
char str[N];
int m,n,k;
clrscr();
printf("***input the original string
***\n");
gets(str);
printf("***The Original string ***\n");
puts(str);
fun(str,&m,&k);
printf("\nThe length of '0' is :
%d\n",m);
printf("*** The suffix of character
***\n");
printf(" %d,%d",k-m+1,k);
}
第3题:
请补充fun()函数,该函数的功能是:把字符的ASCII码中为奇数的字符从字符串str中删除,结果仍然保存在字符串str中,字符串str从键盘输人,其长度作为参数传人fun()函数。
例如,输入“abcdef”,则输出“bdf”。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在mare()函数的横线上填入所编写的若干表达式或语句。
试题程序:
【解析】先判断ASCⅡ码的奇偶。
第一空:根据主函数“fun(str,len);”调用语句,其中len为字符串str的长度,用来控制循环。根据主函数中对应的调用语句,补全fun()函数定义,第一空填“fun(chars[],intn)”。
第二空:“s[j++]=s[i];”语句中j的变量在使用前要对其取值进行初始化,因为数组下标是由0开始的,初始化值为0,所以第二空填“j-0”。
第三空:新生成的字符串要加尾符标志,所以第三空填“s[j]=ˊ\0ˊ”。
第4题:
函数fun的功能是将一个数字字符串转换为一个整数,请将函数补充完整。
int fun(char*str){
int hum=0;
while(*str){
num*=10;
num+=______
str++;
}
return num;
}
第5题:
请补充函数fun(),该函数的功能是:把一个字符串中的字符(字母)按从小到大排序,并把这个全部由字母组成的字符串保存在原串中,函数返回这个字符串的长度。例如;输入“cixbr2.3”,如果为bcirx,字符串长度为5。
注意:部分源程序给出如下.
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
include <stdio.h>
define N 20
int fun (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<='z'))
【 】
p++;
}
*(str+i)='\0';
【 】;
while(*(p+j))
{
k=j;
【 】;
while (*(p+k))
{
if(*(p+k)<*(str+m)
{
t=*(str+m);
*(str+m)=*(p+k);
*(p+k)=t;
}
k++;
}
j++;
}
return i; }
main()
{
char str[81];
iht n;
clrscr();
printf("Input the original string ");
gets(str);
printf("*** The Original string ***In");
puts(str);
printf("*** The nwe string ***\n");
n=fun(str);
puts(str);
printf("***The length of new string is:
%d***\n",n);
}