当前位置:首页 > 中国石油大学C语言答案
#define M 3 #define N 4
void fun(char p[][N],char q[]) { int i,j,count=0; for(j=0;j for(i=0;i int main() { char a[100],w[M][N]={{'W','W','W','W'},{'S','S','S','S'},{'H','H','H','H'}}; fun(w,a); puts(a); return 0; } 7.8 删除字符串中指定的星号 假定输入的字符串中只包含字母和 * 号。请编写函数 fun ,它的功能是:除了尾部的 * 号之外 , 将字符串中其它 * 号全部删除。在编写函数时,不得使用 C 语言提供的字符串函数。 例如,字符串中的内容为: ****A*BC*DEF*G******* ,删除后 , 字符串中的内容应当是: ABCDEFG******* 。 #include void fun(char *s,char *p) { int i,count=0; for(i=0;s+i<=p;i++) if(*(s+i)!='*') { *(s+count)=*(s+i); count++; } for(s+i==p; *(s+i)!='\\0';i++) *(s+count++)=*(s+i); *(s+count)='\\0'; } int main() { char s[81],*t; gets(s); t=s; while(*t) t++; t--; while(*t=='*') t--; fun( s , t ); puts(s); return 0; } 7.9 统计数字字符 请编写函数 fun ,它的功能是:统计形参 s 所指字符串中数字字符出现的次数,并存放在形参 t 所指的变量中,最后在主函数中输出。例如,字符串s为: abcdef35adgh3kjsdf7 。输出结果为: 4 。 #include int main() { char s[80]=\ int t; gets(s); fun(s,&t); printf(\ return 0; } 7.10 将两个串按要求形成一个新串 给定程序的函数 fun 的功能是:逐个比较 p 、 q 所指两个字符串对应位置中的字符,把 ASCII 值大或相等的字符依次存放到 c 所指数组中,形成一个新的字符串。 例如,若主函数中 a 字符串为: aBCDeFgH ,主函数中 b 字符串为:ABcd ,则 c 中的字符串应为: aBcdeFgH 。 #include void fun(char *p,char *q,char *c) { int i,count=0;char max; for(i=0;*(p+i)!='\\0';i++) { max=*(p+i); if(*(p+i)<*(q+i)) max=*(q+i); *(c+count)=max; count++; } if(strlen(q)>strlen(p)) for(i=count;*(q+i)!='\\0';i++) { *(c+count)=*(q+count); count++; } *(c+count)='\\0'; } int main() { char a[10], b[10], c[80]; gets(a); gets(b); fun(a,b,c); puts(c); return 0; } 7.11 统计子串的个数 请编写函数 fun ,它的功能是:统计 substr 所指子字符串在 str 所指字符串中出现的次数。例如,若str中的字符串为 aaas lkaaas ,子字符串为 as ,则应输出 2 。 #include void fun(char*str,char*substr,int*count) { int i=0; *count=0; for(;*str!=0;str++) { for(i=0;*(substr+i)!=0;i++) if(*(substr+i)!=*(str+i)) break; if(*(substr+i)==0) (*count)++; } } int main() { char str[80],substr[80]; int count; gets(str); gets(substr); fun(str,substr,&count); printf(\ return 0; } 7.12 按要求处理字符串 函数 fun 的功能是:将 s 所指字符串中除了下标为奇数、同时 ASCII 值也为奇数的字符之外,其余的所有字符都删除 , 串中剩余字符所形成的一个新串放在 t 所指的数组中。 例如,若 s 所指字符串中的内容为: \其中字符 A 的 ASCII 码值虽为奇数,但所在元素的下标为偶数,因此必需删除;而字符 1 的 ASCII 码值为奇数,所在数组中的下标也为奇数 , 因此不应当删除,其它依此类推。最后 t 所指的数组中的内容应是: \。 #include int i,count=0; for(i=0;*(s+i)!='\\0';i++) if(i%2!=0&&*(s+i)%2!=0) { *(t+count)=*(s+i); count++; } *(t+count)='\\0'; } int main() { char s[100], t[100]; scanf(\ fun(s, t); printf(\ return 0; } 7.13 求非偶数的除数 请编写函数 fun ,它的功能是:求出能整除形参 x 且不是偶数的各整数 , 并按从小到大的顺序放在 pp 所指的数组中 , 这些除数的个数通过形参 n 返回。 例如,若 x 中的值为 : 35 ,则有 4 个数符合要求,它们是 : 1, 5, 7, 35 。 #include void fun(int x,int *aa,int *y) { int i; *y=0; for(i=1;i<=x;i++) if(x%i==0&&i%2!=0) { *aa=i;
共分享92篇相关文档