写一段C语言,只引用标准库。输入一行以句号(.)结尾的字符串。首先输出将其中大、小写字母颠倒后得到的新句子,接下来依次输出该字符串中字母、空格、数字和其他字符(标点符号等)的数目。
时间: 2024-11-18 11:27:57 浏览: 34
下面是一段简单的C语言程序,它实现了你描述的功能:
#include <stdio.h>
#include <ctype.h> // 用于处理字符类型的函数
void reverse_case(char *str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
if (islower(str[i])) { // 将大写转小写
str[i] = toupper(temp);
} else if (isupper(str[i])) { // 将小写转大写
str[i] = tolower(temp);
}
temp = str[len - 1 - i]; // 保存需要交换的字符
if (islower(str[len - 1 - i])) {
str[len - 1 - i] = tolower(temp);
} else if (isupper(str[len - 1 - i])) {
str[len - 1 - i] = toupper(temp);
}
}
}
void count_chars(char *str) {
int alpha_count = 0, space_count = 0, digit_count = 0, other_count = 0;
for (char c : str) {
if (isalpha(c)) {
alpha_count++;
} else if (isspace(c)) {
space_count++;
} else if (isdigit(c)) {
digit_count++;
} else {
other_count++;
}
}
printf("颠倒后的句子: %s\n", str);
printf("字母数: %d\n", alpha_count);
printf("空格数: %d\n", space_count);
printf("数字数: %d\n", digit_count);
printf("其他字符数: %d\n", other_count);
}
int main() {
char input[100];
printf("请输入一行以句点(.)结尾的字符串: ");
fgets(input, sizeof(input), stdin); // 读取一行字符串
input[strcspn(input, "\n")] = '\0'; // 移除行尾换行符
reverse_case(input);
count_chars(input);
return 0;
}
运行这个程序,按照提示输入字符串即可看到结果。