用C语言做,找小写字母,本题目要求读入不含空格的字符串,然后输出这个字符串中小写字母的个数。输入格式:输入在一行中给出一个不含空格的字符串。输入保证字符串长度不超过100。输出格式:输出有两行。第一行按输入顺序输出这个字符串中所有小写字母第二行输出这些小写字母的个数。
时间: 2024-03-24 17:37:50 浏览: 19
以下是用C语言实现的代码:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXN 100
int main()
{
char str[MAXN+1];
fgets(str, MAXN+1, stdin); // 读入不含空格的字符串
int len = strlen(str);
if (str[len-1] == '\n')
{
str[len-1] = '\0'; // 将换行符替换为字符串结束符
len--; // 字符串长度减1
}
int count = 0;
for (int i = 0; i < len; i++)
{
if (islower(str[i]))
{
putchar(str[i]); // 输出小写字母
count++;
}
}
printf("\n%d", count); // 输出小写字母的个数
return 0;
}
```
输入样例:
```
Hello, World!
```
输出样例:
```
elloorld
8
```
相关问题
C语言本题目要求读入不含空格的字符串,然后输出这个字符串中小写字母的个数
。实现思路:
1. 读入不含空格的字符串;
2. 遍历字符串,判断每个字符是否为小写字母;
3. 如果是小写字母,计数器加1;
4. 输出计数器的值。
代码如下:
```c
#include <stdio.h>
int main() {
char str[100];
int count = 0;
printf("请输入一个不含空格的字符串:");
scanf("%s", str);
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
count++;
}
}
printf("小写字母的个数为:%d\n", count);
return 0;
}
```
c语言从键盘读入一个字符串(包括空格),然后统计该字符串的数字、大写字母、小写字母以及空格字符的个数。
下面是一个简单的C语言程序,可以实现从键盘读入一个字符串,然后统计该字符串的数字、大写字母、小写字母以及空格字符的个数。
```
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, digits = 0, uppercase = 0, lowercase = 0, spaces = 0;
printf("请输入一个字符串:");
gets(str);
for(i = 0; str[i] != '\0'; i++) {
if(isdigit(str[i])) {
digits++;
} else if(isupper(str[i])) {
uppercase++;
} else if(islower(str[i])) {
lowercase++;
} else if(isspace(str[i])) {
spaces++;
}
}
printf("数字个数:%d\n", digits);
printf("大写字母个数:%d\n", uppercase);
printf("小写字母个数:%d\n", lowercase);
printf("空格字符个数:%d\n", spaces);
return 0;
}
```
这个程序中,我们首先定义了一个字符数组 `str`,用于存储从键盘读入的字符串。然后定义了四个变量 `digits`, `uppercase`, `lowercase` 和 `spaces`,分别用于统计字符串中的数字、大写字母、小写字母以及空格字符的个数,初始值都为0。
接下来,我们使用 `gets()` 函数从键盘读入字符串,并将其存储到字符数组 `str` 中。然后使用 `for` 循环遍历字符串中的每个字符,对于每个字符,我们使用 `isdigit()`、`isupper()`、`islower()`、`isspace()` 函数判断其是否为数字、大写字母、小写字母或空格字符,如果是,则对应的变量加1。
最后,输出统计结果即可。
需要注意的是,`gets()` 函数不安全,容易导致缓冲区溢出,建议使用更安全的替代函数 `fgets()`。另外,本程序中没有对输入字符串的长度进行检查,如果输入的字符串长度超过了字符数组 `str` 的长度,就会导致程序异常。因此,在实际应用中需要对输入字符串的长度进行检查和处理。