文章目錄
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;
}
及時獲取更多資源請關注微信公衆號: