*p=*p/10; count++; } *p=0; for(j=0;j int main( ) { int n=-1; while(n>99999999||n<0) { scanf(\ } fun(&n); printf(\ return 0; } 7.6 字符串转换成整数 请编写一个函数fun,它的功能是:将一个数字字符串转换为一个整数。例如,若输入字符串\则函数把它转换为整数值 -1234。函数fun中给出的语句仅供参考。 #include int main() { char s[10]; int n; gets(s); n = fun(s); printf(\ return 0; } 7.7 二维数组按列优先存放 请编写函数fun, 函数的功能是: 将M行N列的二维数组中的字符数据, 按列的顺序依次放到一个字符串中。 例如, 二维数组中的数据为: W W W W S S S S H H H H 则字符串中的内容应是: WSHWSHWSHWSH。 #include 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 #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 求非偶数的除数 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库中国石油大学C语言上机题答案(2013版)答案 最全最详细(8)在线全文阅读。
相关推荐: