编程题:
38.请编写函数fun,它的功能是:求出ss所指字符串中指定字符的个数,并返回此值。
例如,若输入字符串123412132,输入字符1,则输出3。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
#define M 81
int fun(char *ss, char c)
{
}
main()
{
char a[M], ch;
clrscr();
printf(“\nPlease enter a string:”);
gets(a);
printf(“\nPlease enter a char:”);
ch = getchar();
printf(“\nThe number of the char is: %d\n”, fun(a, ch));
}
39.请编写函数fun,该函数的功能是:移动一维数组中的内容;若数组中有n个整数,要求把下标从0到p(p小于等于n-1)的数组元素平移到数组的最后。
例如,一维数组中的原始内容为:1,2,3,4,5,6,7,8,9,10;p的值为3。移动后,一维数组中的内容应为:5,6,7,8,9,10,1,2,3,4。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#define N 80
void fun(int *w,int p,int n)
{
}
main()
{
int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int i,p,n=15;
printf(“The original data:\n”);
for(i=0; i
printf(“%3d”,a[i]);
printf(“\n\nEnter p: “);
scanf(“%d”,&p);
fun(a,p,n);
printf(“\nThe data after moving :\n”);
for(i=0; i
printf(“%3d”,a[i]);
printf(“\n\n”);
}
改错题:
74.下列给定程序中,函数fun的功能是:利用插入排序法对字符串中的字符按从小到
大的顺序进行排序。插入法的基本算法是:先对字符串中的头两个元素进行排序。然后把第三个字符插入到前两个字符中,插入后前三个字符依然有序;再把第四个字符插入到前三个字符中,……。待排序的字符串已在主函数中赋予。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#define N 80
#include “stdio.h”
#include “string.h”
void insert(char *aa)
{
int i,j,n;
char ch;
n=strlen(aa);
for(i=1; i
{
/********found********/
c=aa[i];
j=i-1;
while ((j>=0) && (ch
{
aa[j+1]=aa[j];
j–;
}
aa[j+1]=ch;
}
}
main( )
{
char a[N]=”QWERTYUIOPASDFGHJKLMNBVCXZ”;
int i;
printf(“The original string : %s\n”,a);
insert(a);
printf(“The string after sorting : %s\n\n”,a);
}
79.下列给定程序中函数fun的功能是:删除字符串s中的所有空白字符(包括Tab字符、回车符及换行符)。输入字符串时用“#”结束输入。
请改正程序中的错误,使它能输出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include
#include
#include
void fun(char *p)
{
int i,t;
char c[80];
for(i=0,t=0;p[i];i++)
if(!isspace(*(p+i)))
c[t++]=p[i];
/********found********/
c[t]=”\0″;
strcpy(p,c);
}
main( )
{
char c,s[80];
int i=0;
printf(“Input a string: “);
c=getchar();
while(c!=’#')
{
s[i]=c;
i++;
c=getchar();
}
s[i]=’\0′;
fun(s);
puts(s);
}