用c语言多行输入,每行一个仅由大写字母组成的字符串,求其中CE的数量
时间: 2024-05-09 18:20:55 浏览: 13
可以使用scanf函数结合循环来实现多行输入,然后使用字符串的遍历和比较来统计CE的数量。
以下是示例代码:
```c
#include<stdio.h>
#include<string.h>
int main()
{
int n, count;
char str[100];
scanf("%d", &n); //输入字符串行数
count = 0; //计数器初始化为0
while(n--)
{
scanf("%s", str); //输入字符串
for(int i=0; i<strlen(str); i++) //遍历字符串
{
if(str[i] == 'C' && str[i+1] == 'E') //判断是否为CE
{
count++; //计数器加1
}
}
}
printf("%d\n", count); //输出CE的数量
return 0;
}
```
相关问题
C语言输入一行字符串,统计大写英文字母及其数量
好的,我理解了你的问题。下面是C语言的代码,可以实现输入一行字符串,统计大写英文字母及其数量:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int count = 0;
printf("请输入一行字符串:");
fgets(str, 100, stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
count++;
}
}
printf("大写英文字母的数量为:%d\n", count);
return 0;
}
```
代码解释:
- `fgets(str, 100, stdin)` 用来读取用户输入的一行字符串,存储在字符数组 `str` 中;
- `isupper(str[i])` 判断字符 `str[i]` 是否为大写字母,如果是,则计数器 `count` 自增 1;
- `printf` 用来输出结果。
注意,这段代码只统计纯大写字母,如果要统计大小写字母、数字或其它字符数量,可以稍作修改。
输入一个由若干个大写字母组成的字符串,输出其中包含的字符'X'的个数。用C语言
好的,以下是一段 C 语言代码实现:
```c
#include <stdio.h>
int main() {
char s[100];
int count = 0;
scanf("%s", s);
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == 'X') {
count++;
}
}
printf("%d", count);
return 0;
}
```
首先定义一个字符数组,用于存储输入的字符串。然后使用 `scanf` 函数从标准输入中读取字符串。接着使用循环遍历字符串中的每个字符,如果这个字符是'X',就把计数器加1。最后使用 `printf` 函数输出计数器的值即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)