求解这几道C语言题的答案

来源:百度知道 编辑:UC知道 时间:2024/06/16 00:07:59
一、程序填空题 请补充函数proc,该函数的功能是按条件删除一个字符串指定字符一半的数目,具体要求如下:如果该字符串所包含的指定字符的个数是奇数,则不删除,如果其数目是偶数,则删除原串后半部分的指定字符。其中,str指向原字符串,删除后的字符串存放在b所指的数组中,c中存放指定的字符。例如,当str输入“abcabcabcab”,c=‘b’时,b的输出为“abcabcaca”;如果str的输入为“abcabcabca”,则b的输出为“abcabcabca”。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数proc的横线上填入所编写的若干表达式或语句。 试题程序: #include <stdlib.h> #include <stdio.h> #include <conio.h> #define M 80 void proc(char str〔〕,char b〔〕,char c) { int i=0,j=0; int n=0; int m=0; while(str〔i〕!=‘\0’) { if(str〔i〕==c) n++; i++; } 【1】; if(n%2) { while(str〔j〕!=‘\0’) { b〔j〕=str〔j〕; j++; } b〔j〕=‘\0’; } else { while(str〔i〕!=‘\0’) { b〔j++〕=str〔i〕; if(str〔i〕==c) m++; if((m>n/2)&&(str〔i〕==c)) 【2】; i++; } 【3】; } } void main() { char str〔M〕,b〔M〕; char c; system("CLS"); printf("Enter the string:\n"); gets(str); printf("Enter the character of the string deleted:"); scanf("%c",&c); proc(str,b,c); printf("The new string is : %s\n",b); }

同志,拜托把问题说清楚一点,你的解释前后矛盾,让我不知所云,
对不起,刚才没看清楚,请别介意;答案我已经做出来了:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define M 80
void proc(char str[],char b[],char c)
{int i=0,j=0;
int n=0;
int m=0;
while(str[i]!='\0')
{if(str[i]==c)
n++;
i++;
}
i=0;/*第一个空*/
if(n%2)
{while(str[j]!='\0')
{b[j]=str[j];
j++;
}
b[j]='\0';
}
else {while(str[i]!='\0')
{b[j++]=str[i];
if(str[i]==c)
m++;
if((m>n/2)&&(str[i]==c))
break;/*第二个空*/
i++;}
b[j-1]='\0';/*第三个空*/
}
}
void main()
{char str[80],b[80];
char c;
system("CLS");
printf("Enter the string:\n");
gets(str);
printf("Enter the characer of the string deleted:");
scanf("%c",&c);
proc(str,b,c);
printf("The new str