文章目录
031 查找字符并删除
【题目】请编写函数fun,其功能是:将s所指字符串中除了下标为偶数、同时ASCII值也为偶数的字符外,其余的全都删除;串中剩余字符所形成的一个新串放在t所指的一个数组中。
【代码】
#include <stdio.h>
#include <string.h>
void fun(char *s, char t[]) {
int i, j = 0, n = strlen(s);
for (i = 0; i < n; i++) {
if (i % 2 == 0 && s[i] % 2 == 0) {
t[j++] = s[i];
}
}
t[j] = '\0';
}
//测试
int main() {
char s[100];
char t[100];
gets(s);
fun(s, t);
puts(t);
return 0;
}
032 查找字符并删除
【题目】请编写函数fun,其功能是:将s所指字符串中除了下标为奇数、同时ASCII值也为奇数的字符之外,其余的所有字符都删除,串中剩余字符所形成的一个新串放在t所指的一个数组中。
【代码】
#include <stdio.h>
#include <string.h>
void fun(char *s, char t[]) {
int i, j = 0, n = strlen(s);
for (i = 0; i < n; i++) {
if (i % 2 != 0 && s[i] % 2 != 0) {
t[j++] = s[i];
}
}
t[j] = '\0';
}
int main() {
char s[100];
char t[100];
gets(s);
fun(s, t);
puts(t);
return 0;
}
033 将字符串中的前导 * 号全部删除
【题目】假定输入的字符串中只包含字母和 * 号。请编写函数 fun(),它的功能是:将字符串中的前导 * 号全部删除,中间和后面的 * 号不删除。
【代码】
#include <stdio.h>
#include <string.h>
void fun(char *a) {
char *p=a;
while (*p== '*') {
p++;
}
strcpy (a,p);
}
int main() {
char a[100];
gets(a);
fun(a);
puts(a);
return 0;
}
035 把分数最高的学生数据放在h所指的数组中
【题目】学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能使:把分数最高的学生数据放在h所指的数组中,注意:分数最高的学生可能不止一个,函数返回分数最高的学生的人数。
【代码】
#include <stdio.h>
#include <stdlib.h>
#define N 3
typedef struct student {
int id;
double score;
} Student;
int fun(Student s[], Student t[] ) {
double max;
int i, j = 0;
max = s[0].score;
for (i = 1; i < N; i++) {
if (s[i].score > max) {
max = s[i].score;
}
}
for (i = 0; i < N; i++) {
if (s[i].score == max) {
t[j].score = s[i].score;
t[j].id = s[i].id;
j++;
}
}
return j;
}
int main() {
Student s[N];
double score;
int id;
int i;
for (i = 0; i < N; i++) {
scanf("%d %lf", &id, &score);
s[i].score = score;
s[i].id = id;
}
Student t[100];
printf("%d", fun(s, t));
return 0;
}
035 删除字符串中的所有空格
【题目】请编写一个函数,用来删除字符串中的所有空格。
【代码】
#include <stdio.h>
void fun(char *str) {
int i = 0;
char *p = str;
while (*p) {
if (*p != ' ') {
str[i++] = *p;
}
p++;
}
str[i] = '\0';
}
int main() {
char a[100];
gets(a);
fun(a);
puts(a);
return 0;
}
036 将字符串中的前导 * 号全部移到字符串的尾部。
【题目】假定输入的字符串中只包含字母和 * 号。请编写函数fun,它的功能是:将字符串中的前导 * 号全部移到字符串的尾部。
【代码】
有点重了,就不写了…
037 求出该学生的平均分放在记录的ave成员中
【题目】某学生的记录由学号、8门课程成绩和平均分组成,学号和8门课程的成绩已在主函数中给出。请编写函数fun,它的功能是:求出该学生的平均分放在记录的ave成员中。请自己定义正确的形参。
【代码】
思路看 35 题
038 求出ss所指字符串中指定字符的个数,并返回此值。
【题目】请编写函数fun,它的功能是:求出ss所指字符串中指定字符的个数,并返回此值。
【代码】
#include <stdio.h>
int fun(char *a, char c) {
int n = 0;
while (*a) {
if (*a == c) {
n++;
}
a++;
}
return n;
}
int main() {
char a[100];
gets(a);
int num = fun(a, 'c');
printf("%d", num);
return 0;
}
039 移动一维数组中的内容
【题目】请编写函数fun,该函数的功能是:移动一维数组中的内容,若数组中由n个整数,要求把下标从0到p(p小于等于n-1)的数组元素平移到数组的最后。
【代码】
和 40 题差不多
040 移动字符串中内容
【题目】请编写函数fun,该函数的功能是移动字符串中内容,移动的规则如下:把第1到第m个字符,平移到字符串的最后,把第m+1到最后的字符移到字符串的前部。
【代码】
#include <stdio.h>
#include <string.h>
#define N 100
void fun(char *w, int m) {
char b[N];
int n = strlen(w);
int i, j = 0;
for (i = 0; i < m; i++) {
b[j] = w[i];
j++;
}
for (i = 0; i < n - m; i++) {
w[i] = w[i + m];
}
for (j = 0; j < m; j++) {
w[i++] = b[j];
}
w[i] = '\0';
}
int main() {
char a[N];
gets(a);
fun(a, 3);
puts(a);
return 0;
}
及时获取更多资源请关注微信公众号: