用c语言编写一个程序,要求用户输入一个字符串,该程序将计数并输出该字符串中连续重复字符的最大数量。
时间: 2023-05-22 13:04:45 浏览: 86
您好,以下是代码实现:
```
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int maxCount = 0;
int currentCount = 1;
printf("请输入一个字符串:\n");
scanf("%s", str);
int len = strlen(str);
for (int i = 1; i < len; i++) {
if (str[i] == str[i-1]) {
currentCount++;
} else {
if (currentCount > maxCount) {
maxCount = currentCount;
}
currentCount = 1;
}
}
if (currentCount > maxCount) {
maxCount = currentCount;
}
printf("该字符串中连续重复字符的最大数量为:%d\n", maxCount);
return 0;
}
```
如果您有其它问题可以继续提问。
相关问题
用c语言编写一个程序,输入一个字符串,输出该字符串中出现次数为奇数的字符及其出现次数
以下是用C语言编写的程序,实现输入一个字符串,输出该字符串中出现次数为奇数的字符及其出现次数:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int count[256] = {0};
int i, len;
printf("请输入一个字符串:");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len; i++)
{
count[(int)str[i]]++;
}
printf("出现次数为奇数的字符及其出现次数为:\n");
for (i = 0; i < 256; i++)
{
if (count[i] % 2 == 1)
{
printf("%c:%d\n", i, count[i]);
}
}
return 0;
}
```
程序的实现思路如下:
1. 首先定义一个字符数组和一个计数数组。字符数组用于存储输入的字符串,计数数组用于统计每个字符出现的次数。
2. 然后使用 `scanf()` 函数获取用户输入的字符串,并使用 `strlen()` 函数获取字符串的长度。
3. 接下来使用一个循环遍历字符串中的每个字符,将每个字符的 ASCII 码作为计数数组的下标,对应元素的值加 1。
4. 最后再使用一个循环遍历计数数组,找到出现次数为奇数的字符,并输出该字符和其出现次数。
需要注意的是,此程序默认输入的字符串中只包含 ASCII 码表中的字符。如果输入的字符串中包含其他字符,需要根据实际情况做相应的修改。
c语言编写一个程序,接受用户输入的一个字符串,然后统计并输出字符串中出现次数最多的字母及其出现次数。
好的,以下是一个用C语言编写的程序,可以接受用户输入的字符串,并统计并输出字符串中出现次数最多的字母及其出现次数:
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main() {
char str[MAX_SIZE];
int count[26] = {0}; // 用于统计每个字母出现的次数
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
count[str[i] - 'a']++;
} else if (str[i] >= 'A' && str[i] <= 'Z') {
count[str[i] - 'A']++;
}
}
int max_count = 0;
char max_char;
for (int i = 0; i < 26; i++) {
if (count[i] > max_count) {
max_count = count[i];
max_char = 'a' + i;
}
}
printf("出现次数最多的字母是 %c,出现次数为 %d\n", max_char, max_count);
return 0;
}
```
这个程序中,我们首先定义了一个大小为 `MAX_SIZE` 的字符数组 `str` 来存储用户输入的字符串,另外还定义了一个大小为 26 的整型数组 `count` 来统计每个字母出现的次数。然后,我们使用 `fgets` 函数从标准输入中读取用户的输入,并将其存储到 `str` 中。
接下来,我们遍历字符串的每个字符,如果字符是小写字母,则将对应的计数数组 `count` 的元素加1;如果字符是大写字母,则同样将对应的计数数组元素加1。
最后,我们遍历计数数组 `count`,找到出现次数最多的字母及其出现次数,并将其输出给用户。
希望对你有所帮助!如果有任何疑问,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)