|
发表于 2023-4-23 15:01:20
|
显示全部楼层
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- void remove_non_alpha(char *str) {
- int i = 0, j = 0;
- while (str[i]) {
- if (isalpha(str[i])) {
- str[j++] = str[i];
- }
- i++;
- }
- str[j] = '\0';
- }
- void to_upper(char *str) {
- int i = 0;
- while (str[i]) {
- str[i] = toupper(str[i]);
- i++;
- }
- }
- int main() {
- char str[5][80];
- // 1. 输入5个字符串
- for (int i = 0; i < 5; i++) {
- printf("请输入字符串 %d: ", i + 1);
- fgets(str[i], sizeof(str[i]), stdin);
- str[i][strcspn(str[i], "\n")] = 0; // 去掉末尾的换行符
- }
- // 2. 保留英文字母并删除其他字符
- for (int i = 0; i < 5; i++) {
- remove_non_alpha(str[i]);
- }
- // 3. 将小写字母转换为大写字母
- for (int i = 0; i < 5; i++) {
- to_upper(str[i]);
- }
- // 4. 输出处理后的字符串
- printf("\n处理后的字符串:\n");
- for (int i = 0; i < 5; i++) {
- printf("%s\n", str[i]);
- }
- return 0;
- }
复制代码
这段代码定义了两个辅助函数 `remove_non_alpha` 和 `to_upper`,分别实现题目中要求的功能。`remove_non_alpha` 函数删除字符串中的非英文字母字符,`to_upper` 函数将字符串中的小写字母转换为大写字母。`main` 函数中依次实现了题目要求的输入、处理和输出过程。 |
|