问题14_1
函数 f u n fun fun 的功能是:在形参 s s ss ss 所指字符串数组中查找与形参 t t t 所指字符串相同的串,找到后返回该串在字符串数组中的位置(即下标值),若未找到则返回 − 1 -1 −1 。 s s ss ss 所指字符串数组中共有 N N N 个内容不同的字符串,且串长小于 M M M 。
代码14_1
#include<stdio.h>
#include<string.h>
#define N 5
#define M 8
int fun(char (*ss)[M], char *t){
int i;
for(i=0; i<N; i++){
if(strcmp(ss[i], t)==0)
return i;
}
return (-1);
}
void main(void){
char ch[N][M] = {"if", "while", "switch", "int", "for"}, t[M];
int n, i;
printf("\nThe original string\n\n");
for(i=0; i<N; i++)
puts(ch[i]);
printf("\n");
printf("\nEnter a string for search:");
gets(t);
n = fun(ch, t);
if(n==-1)
printf("\nDon't found!\n");
else
printf("\nThe position is %d\n", n);
}
结果14_1

问题14_2
函数
f
u
n
fun
fun的功能是:从整数
10
˜
55
10\ \~\ 55
10 ˜55 ,查找能被
3
3
3 整除且有一位上的数值为
5
5
5 的数,把这些数放在
b
b
b 所指的数组中,这些数的个数作为函数值返回。规定函数中
a
1
a1
a1 放个位数,
a
2
a2
a2 放十位数。
例如,若给
n
=
5
n=5
n=5,则该项的斐波那契数值为
13
13
13。
代码14_2
#include<stdio.h>
int fun(int *b){
int k, a1, a2, i=0;
for(k=10; k<=55; k++){
a2 = k/10;
a1 = k - a2*10;
if((k%3==0&&a2==5)||(k%3==0&&a1==5)){
b[i] = k;
i++;
}
}
return i;
}
void main(void){
int a[100], k, m;
m = fun(a);
printf("The result is:\n");
for(k=0; k<m; k++)
printf("%4d", a[k]);
printf("\n");
}
结果14_2

问题14_3
规定输入字符串中指包含字母和
∗
*
∗ 号。请编写函数
f
u
n
fun
fun ,其功能是:将字符串尾部的
∗
*
∗ 号全部删除,前面和中间的
∗
*
∗ 不动。
例如,字符串的内容为
∗
∗
∗
∗
A
B
C
∗
∗
D
E
∗
∗
F
∗
∗
∗
∗
****ABC**DE**F****
∗∗∗∗ABC∗∗DE∗∗F∗∗∗∗ ,输出为
∗
∗
∗
∗
A
B
C
∗
∗
D
E
∗
∗
F
****ABC**DE**F
∗∗∗∗ABC∗∗DE∗∗F 。
代码14_3
#include<stdio.h>
#include<conio.h>
void fun(char *a){
while(*a!='\0')
a++;
a--; // 指针 a 指向字符串的尾部
while(*a == '*')
a--; // 指针 a 执行最后一个字母
*(a+1) = '\0'; // 在字符串最后加上结束标志符
}
void main(void){
char s[81];
printf("Enter a string:\n");
gets(s);
fun(s);
printf("The string after deleted:\n");
puts(s);
}
结果14_3


被折叠的 条评论
为什么被折叠?



