一. 程序题(共5题,100分)
1. (程序题)
题目编号:Exp05-Basic01,GJBook3-07-06
题目名称:字符串长度
题目描述:编写程序实现:使用自编函数int strlen(char *str),求字符串str长度;并在主程序中调用。
输入:从键盘随机输入长度不超过256的字符串,字符串内可能包含若干空白字符。
输出:字符串的长度。
样例1:
输入:123 456
输出:8
样例2:
输入:A
输出:1
#include<stdio.h>
#include<stdlib.h>
int strlen(char* str) {
for (int i = 0;; i++) {
if (*str == '\0') { return i; }
str++;
}
}
int main() {
char ch[256];
gets(ch);
printf("%d", strlen(ch));
}
2. (程序题)
题目编号:Exp05-Basic02,GJBook3-07-14
题目名称:分类统计字符
题目描述:
编写程序实现:使用自定义函数void count(char * str, int * upper, int *lower, int *space, int *num, int*other)统计给定字符串str中的大写字母、小写字母、空格、数字字符、其它字符的数目,并以指针参数传回主程序中的调用点,并输出。
输入:从键盘随机输入一个长度不超过256的字符串,字符串内可能包含若干空白字符。
输出:大写字母字符个数、小写字母字符个数、空格字符个数、数字字符个数和其它字符个数,数字间以一个西文空格间隔,最后一个数后无多余字符。
样例:
输入: 123 abc ABC
输出: 3 3 5 3 0
#include<stdio.h>
#include<stdlib.h>
void count(char* str, int* upper, int* lower, int* space, int* num, int* other) {
while (*str != '\0') {
if (*str <= 'Z' && *str >= 'A')
(* upper)++;
else if (*str <= 'z' && *str >= 'a')
(* lower)++;
else if (*str <= '9' && *str >= '0')
(* num)++;
else if (*str == ' ')
(* space)++;
else
(* other)++;
str++;
}
}
int main() {
char str[256];
int upper, lower, space, num, other;
upper = lower = space = num = other = 0;
gets(str);
count(str, &upper, &lower, &space, &num, &other);
printf("%d %d %d %d %d", upper, lower, space, num, other);
}
3. (程序题)
题目编号:Exp05-Basic03,GJBook3例-07-01
题目名称:排序二维数组
题目描述:编写程序对输入的二维整型矩阵进行递增排序。
输入:第一行输入两个正整数n和m (n, m<=50)分别对应矩阵的行数和列数。随后n行,每行输入m个整数,每个整数间以空格间隔。
输出:排序后的矩阵:共n行,每行m个整数,之间以一个西文空格间隔,每行最后一个数字后除必要的回车换行符无其它字符,最后一行末尾无任何多余字符。
样例:
输入: 2 3 9 6 5 1 2 3
输出: 1 2 3 5 6 9
#include<stdio.h>
#include<stdlib.h>
int n, m;
void mp(int* p) {
int* i = p, j, k, r, flag = 1;
while (flag) {
flag = 0;
i = p;
while ((i - p) < n * m - 1) {
if ((*i) > *(i + 1)) {
r = *i;
*i = *(i + 1);
*(i + 1) = r;
flag = 1;
}
i++;
}
}
}
int main() {
int a[50][50];
scanf("%d%d", &n, &m);
for (int i = 0; i < n * m; i++) {
scanf("%d", &a[0][i]);
}
mp(&a[0][0]);
for (int s = 0; s < n * m; s++) {
if ((s + 1) % m == 0) { printf("%d\n", a[0][s]); }
else if (s == n * m - 1) { printf("%d", a[0][s]); }
else { printf("%d ", a[0][s]); }
}
}
4. (程序题)
题目编号 :Exp05-Basic04,GJBook3-07-11
题目名称:字符串部分删除
题目描述:
编写程序实现:使用自编函数char * str_delete(char *s, int v, int w)从字符串s的第v个字符开始删除w个字符,并将处理后的字符串首地址以函数返回值带回调用点。
输入:
第一行输入两个整数分别对应 v和w(0≤v, w<MIN(256,strlen(s))),测试数据已保证v和w符合要求;
第二行输入一个长度不超过256的字符串,字符串可能含有空格。
输出:
处理后的字符串。
样例:
输入: 2 6 ABCDEFGH 12345
输出: AH 12345
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* str_delete(char* s, int v, int w) {
s += v-1;
while (*(s + w) != '\0') {
*s = *(s + w);
s++;
}
return &s[0];
}
int main() {
char s[300],*z=s;
int v, w;
scanf("%d%d", &v, &w);
char ch = getchar();//无语😶样例的6后面居然有个空格
gets(s);
int m = strlen(s);
str_delete(s,v,w);
for (int i = 0; i < m - w; i++) {
printf("%c", s[i]);
}
}
5. (程序题)
题目编号:Exp05-Enhanc02,freshman-1031
题目名称:合法标识符
题目描述:
已知C语言中合法标识的定义是,以_或字母开始,并由_、数字、字母构成的连续字符串。编写程序判断给定的字符串是否是合法C语言标识符。
输入:输入第一行给出正整数 N(≤10^2)是输入的字符串个数。随后 N 行,每行给出1个任意长度的字符串(可能含有空白字符,长度不超过256)。
输出:
输出第一行以Valid:X 格式给出合法标识符个数 X,第二行以Invalid:Y 格式给出非法标识符个数 Y,无多余字符,冒号为西文冒号。
样例:
输入: 2 1242345567788990 abcdedffesfsgfef
输出: Valid:1 Invalid:1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int judge(char *a,int b) {
char* s = a;
if (*a == '_' || ((*a <= 'Z' && *a >= 'A') || (*a <= 'z') && (*a >= 'a'))) {
a++;
while (a-s<b) {
if (!(((*a >= '0') && (*a <= '9')) || *a == '_' || ( * a <= 'Z' && *a >= 'A') || (*a <= 'z' && *a >= 'a'))) {
return 0;
}
a++;
}
return 1;
}
else { return 0; }
}
int main() {
int n,b,validct = 0,invalidct=0;
char a[300];
scanf("%d", &n);
char ch = getchar();
for (int i = 0; i < n; i++) {
gets(a);
b = strlen(a);
if (judge(a, b)) {
validct++;
}
else { invalidct++; }
}
printf("Valid:%d\nInvalid:%d", validct, invalidct);
}