石油大c语言答案,中国石油大学C语言答案
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 刪除字符串中指定的星號(hào)
假定輸入的字符串中只包含字母和 * 號(hào)。請(qǐng)編寫(xiě)函數(shù) fun ,它的功能是:除了尾部的 * 號(hào)之外 , 將字符串中其它 * 號(hào)全部刪除。在編寫(xiě)函數(shù)時(shí),不得使用 C 語(yǔ)言提供的字符串函數(shù)。 例如,字符串中的內(nèi)容為: ****A*BC*DEF*G******* ,刪除后 , 字符串中的內(nèi)容應(yīng)當(dāng)是: 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 統(tǒng)計(jì)數(shù)字字符
請(qǐng)編寫(xiě)函數(shù) fun ,它的功能是:統(tǒng)計(jì)形參 s 所指字符串中數(shù)字字符出現(xiàn)的次數(shù),并存放在形參 t 所指的變量中,最后在主函數(shù)中輸出。例如,字符串s為: abcdef35adgh3kjsdf7 。輸出結(jié)果為: 4 。 #include void fun(char *p,int *q) { int i,count=0; for(i=0;*(p+i)!='\\0';i++) if(*(p+i)>='0'&&*(p+i)<='9') count++; *q=count; }
int main()
{ char s[80]=\ int t; gets(s); fun(s,&t);
printf(\ return 0; }
7.10 將兩個(gè)串按要求形成一個(gè)新串
給定程序的函數(shù) fun 的功能是:逐個(gè)比較 p 、 q 所指兩個(gè)字符串對(duì)應(yīng)位置中的字符,把 ASCII 值大或相等的字符依次存放到 c 所指數(shù)組中,形成一個(gè)新的字符串。
例如,若主函數(shù)中 a 字符串為: aBCDeFgH ,主函數(shù)中 b 字符串為:ABcd ,則 c 中的字符串應(yīng)為: 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)
*(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 統(tǒng)計(jì)子串的個(gè)數(shù)
請(qǐng)編寫(xiě)函數(shù) fun ,它的功能是:統(tǒng)計(jì) substr 所指子字符串在 str 所指字符串中出現(xiàn)的次數(shù)。例如,若str中的字符串為 aaas lkaaas ,子字符串為 as ,則應(yīng)輸出 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 按要求處理字符串
函數(shù) fun 的功能是:將 s 所指字符串中除了下標(biāo)為奇數(shù)、同時(shí) ASCII 值也為奇數(shù)的字符之外,其余的所有字符都刪除 , 串中剩余字符所形成的一個(gè)新串放在 t 所指的數(shù)組中。 例如,若 s 所指字符串中的內(nèi)容為: \其中字符 A 的 ASCII 碼值雖為奇數(shù),但所在元素的下標(biāo)為偶數(shù),因此必需刪除;而字符 1 的 ASCII 碼值為奇數(shù),所在數(shù)組中的下標(biāo)也為奇數(shù) , 因此不應(yīng)當(dāng)刪除,其它依此類推。最后 t 所指的數(shù)組中的內(nèi)容應(yīng)是: \。
#include #include void fun(char *s,char *t) {
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 求非偶數(shù)的除數(shù)
請(qǐng)編寫(xiě)函數(shù) fun ,它的功能是:求出能整除形參 x 且不是偶數(shù)的各整數(shù) , 并按從小到大的順序放在 pp 所指的數(shù)組中 , 這些除數(shù)的個(gè)數(shù)通過(guò)形參 n 返回。
例如,若 x 中的值為 : 35 ,則有 4 個(gè)數(shù)符合要求,它們是 : 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;
總結(jié)
以上是生活随笔為你收集整理的石油大c语言答案,中国石油大学C语言答案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 博本 微型 电脑 linux,博本电脑下
- 下一篇: c语言枚举变量自增报错,C_数据结构与算