循环(统计两个连续的字母出现的次数)

/*

 

 循环(统计两个连续的字母出现的次数)

 

 编写一个程序读取输入,直到 '\n' ,并报告序列 ei 出现的次数

 

 */


#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[]) {
    
    char str[80];
    int count = 0;
    
    gets(str);
    
    int lenth = (int)strlen(str);
    
    int i;
    for (i = 0; i < lenth - 1; i++) {
        if (str[i] == 'e' && str[i+1] == 'i') {
            count++;
        }
    }
    
    printf("count = %d\n",count);
    
    return 0;
}


回答1: 可以使用Python中的字典来统计字符串中字母出现次数。具体实现步骤如下: 1. 定义一个空字典,用于存储每个字母出现次数。 2. 遍历字符串中的每个字符,如果该字符是字母,则将其加入字典中,并将其出现次数加1。 3. 最后输出字典中每个字母出现次数。 示例代码如下: ```python # 定义字符串 s = 'hello, world!' # 定义空字典 d = {} # 遍历字符串中的每个字符 for c in s: # 如果该字符是字母 if c.isalpha(): # 将其加入字典中,并将其出现次数加1 if c in d: d[c] += 1 else: d[c] = 1 # 输出字典中每个字母出现次数 for k, v in d.items(): print(k, v) ``` 输出结果为: ``` h 1 e 1 l 3 o 2 w 1 r 1 d 1 ``` 回答2: Python是一种动态类型语言,拥有强大的数据统计功能。对于字符串中字母出现次数统计,Python提供了许多实用的工具和方法。 统计字符串字母出现次数的方法有很多,这里主要介绍两种常用的方法:利用Python内置的字符串函数和利用Python的自定义函数。 一、利用Python内置函数 1. count()函数 Python内置的count()函数可以统计字符串中指定字符出现次数。例如,统计字符串"hello world"中字母"o"出现次数,代码如下: ``` string = "hello world" count = string.count("o") print("字母o出现次数:", count) ``` 运行结果为: ``` 字母o出现次数:2 ``` 2. collections模块的Counter类 collections模块是Python标准库中的一个集合模块,提供了许多有用的集合类和函数。其中,Counter类可以用于统计元素出现次数。例如,统计字符串"hello world"中每个字母出现次数,代码如下: ``` from collections import Counter string = "hello world" count = Counter(string) print("每个字母出现次数:", count) ``` 运行结果为: ``` 每个字母出现次数: Counter({'l': 3, 'o': 2, 'e': 1, 'h': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1}) ``` 二、利用Python自定义函数 自定义Python函数可以实现更多的字符串操作,可以自定义指定字符串中字母出现次数的函数,例如: ``` def count_letters(string): letters = {} for letter in string: if letter.isalpha(): if letter in letters: letters[letter] += 1 else: letters[letter] = 1 return letters string = "hello world" count = count_letters(string) print("每个字母出现次数:", count) ``` 运行结果为: ``` 每个字母出现次数: {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1} ``` 该函数使用字典存储每个字母出现次数,其中,字母的大小写不敏感。 总之,Python拥有强大的字符串处理功能,可以轻松实现字符串中字母出现次数统计和处理。以上介绍的方法只是其中的一部分,读者可以根据实际需求选择合适的方法。 回答3: Python 是一门功能强大的编程语言,能够轻松实现对字符串字母出现次数统计。下面,我将详细介绍如何在 Python 中实现此功能。 首先,我们需要定义一个字符串,例如: ```python text = "Hello, world!" ``` 接着,我们可以使用 Python 中的 `collections` 模块中的 `Counter` 类来统计每个字符的出现次数。如下: ```python from collections import Counter counts = Counter(text.lower()) ``` 其中,`lower()` 方法用于将字符串转换为小写字母,便于统计。`Counter` 类接受一个可迭代对象作为参数,返回一个包括每个元素出现次数的字典。 最后,我们可以使用循环语句输出每个字符及其出现次数。如下: ```python for char, count in counts.items(): if char.isalpha(): print(char, count) ``` 其中,`items()` 方法以列表返回可遍历的 (键, 值) 元组数组。`isalpha()` 方法用于判断字符是否是字母。整个代码如下: ```python from collections import Counter text = "Hello, world!" counts = Counter(text.lower()) for char, count in counts.items(): if char.isalpha(): print(char, count) ``` 运行上述代码,即可输出每个字母出现次数。 综上,使用 Python 统计字符串字母出现次数非常简单,只需要利用 `collections` 模块中的 `Counter` 类,结合循环语句输出结果即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值