使用C语言编写程序请输入一行字符串作为作文标题,标题中可能包含大、小写英文字母、数字字符、空格和换行符。统计并输出该标题中有多少个字符,统计标题字符数时,空格和换行符不计算在内。要求使用循环进行输入,不允许用gets函数和scanf语句。写出完整程序代码并给出注释
时间: 2023-03-14 21:53:58 浏览: 134
用C语言编写程序,统计标题中的字符数,请参考如下示例:#include <stdio.h>int main(void) {
char ch;
int count = 0;
printf("请输入一行字符串作为作文标题:\n");
ch = getchar(); // 使用getchar()函数获取标题的每一个字符 // 通过循环,对每一个字符进行字符数的统计,并且排除空格和换行符
while (ch != '\n') {
if (ch != ' ') {
count++;
}
ch = getchar();
} printf("标题中有 %d 个字符\n", count);
return 0;
}
相关问题
用c语言编写,输入一个正整数n,再输入n行字符串,分别统计其中大写英文字母、小写英文字母、数字、空格和其他字符的个数。
在C语言中,你可以通过遍历用户输入的每一行字符串,并利用`isalpha()`, `isdigit()`, 和 `isspace()` 函数来判断字符的类别,然后累加计数。以下是一个简单的程序示例:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
int n;
printf("请输入一个正整数n: ");
scanf("%d", &n);
// 初始化计数器
int upper_case = 0, lower_case = 0, digits = 0, spaces = 0, others = 0;
for (int i = 0; i < n; i++) {
char line[100];
printf("请输入第 %d 行字符串: ", i + 1);
fgets(line, sizeof(line), stdin);
// 跳过换行符
if (line[strlen(line) - 1] == '\n') {
line[strlen(line) - 1] = '\0';
}
// 遍历并分类计数
for (char c : line) {
if (isupper(c)) {
upper_case++;
} else if (islower(c)) {
lower_case++;
} else if (isdigit(c)) {
digits++;
} else if (isspace(c)) {
spaces++;
} else {
others++;
}
}
}
printf("统计结果:\n"
"大写字母: %d\n"
"小写字母: %d\n"
"数字: %d\n"
"空格: %d\n"
"其他字符: %d\n",
upper_case, lower_case, digits, spaces, others);
return 0;
}
```
用C语言编写程序代码:输入一行字符串,统计其中大写字母、小写字母、空格、数字及其他字符的个数,判断其是否回文。回文串即正着读和反着读都相同的字符串
非常感谢您的提问!以下是我为您准备的答案:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[100];
int upper = 0, lower = 0, space = 0, digit = 0, other = 0;
int i, j, len;
printf("请输入一行字符串:");
fgets(str, 100, stdin);
len = strlen(str) - 1;
for (i = 0, j = len - 1; i < j; i++, j--) {
if (str[i] != str[j]) {
break;
}
}
if (i >= j) {
printf("是回文串\n");
} else {
printf("不是回文串\n");
}
for (i = 0; i < len; i++) {
if (isupper(str[i])) {
upper++;
} else if (islower(str[i])) {
lower++;
} else if (isspace(str[i])) {
space++;
} else if (isdigit(str[i])) {
digit++;
} else {
other++;
}
}
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("空格:%d\n", space);
printf("数字:%d\n", digit);
printf("其他字符:%d\n", other);
return 0;
}
```
这是一个用 C 语言编写的程序,可以输入一行字符串,统计其中大写字母、小写字母、空格、数字及其他字符的个数,并判断其是否回文。程序首先使用 fgets 函数读取一行字符串,然后使用 strlen 函数获取字符串长度。接着使用一个 for 循环判断字符串是否回文,如果是回文串,则输出“是回文串”,否则输出“不是回文串”。最后再使用另一个 for 循环统计各种字符的个数,并输出结果。
希望这个程序能够帮助您解决问题!如果您还有其它问题,欢迎随时提出。
阅读全文
相关推荐













