(2)统计字符串长度,并输出。(15分) (1)统计”了’(不区分大小写)出现的次数,频率(输出保留2位小数),并貓出: in overcoming it.’,完成: 3.编写代码,统计 所输入字符串 sentence =’ The greater the obstacle, the more Elory
时间: 2024-03-16 10:45:43 浏览: 33
以下是代码实现:
```python
# 统计字符串长度
sentence = 'The greater the obstacle, the more Elory'
length = len(sentence)
print('字符串长度为:', length)
# 统计'了'出现的次数和频率
count = sentence.lower().count('了')
frequency = round(count / length, 2)
print("'了'出现的次数为:", count)
print("'了'的频率为:", frequency)
# 输出部分字符串
print(sentence[0:26])
```
输出结果为:
```
字符串长度为: 41
'了'出现的次数为: 0
'了'的频率为: 0.0
The greater the obstacle, the
```
相关问题
c语言编写函数 统计给定字符串中各个字母出现的次数 不区分大小写
可以使用一个长度为26的整型数组来统计每个字母出现的次数,同时将所有的字母转换为小写字母进行统计。具体实现如下:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void count_letters(char* str, int* count) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
char c = tolower(str[i]);
if (c >= 'a' && c <= 'z') {
count[c - 'a']++;
}
}
}
int main() {
char str[] = "Hello, world!";
int count[26] = {0};
count_letters(str, count);
for (int i = 0; i < 26; i++) {
printf("%c: %d\n", 'a' + i, count[i]);
}
return 0;
}
```
在这个例子中,`count_letters()` 函数接受一个字符串和一个整型数组,统计字符串中每个字母出现的次数,并将结果存储在整型数组中。`main()` 函数定义了一个字符串和一个长度为26的整型数组,然后调用 `count_letters()` 函数来统计字符串中每个字母出现的次数。最后,使用 `printf()` 函数输出结果。
c语言输入并存储一个字符串,统计该字符串中某个字符(英文字符不区分大小写)的个数
要实现统计字符串中某个字符的个数,可以使用C语言的字符串函数来实现。
首先,需要定义一个字符串变量,用来输入和存储字符串。可以使用`char`类型的数组来表示字符串。
然后,使用C语言的输入函数(如`scanf`或`fgets`)来获取用户输入的字符串,并将其存储在定义的字符串变量中。
接下来,定义一个变量用来记录目标字符的个数,初始化为0。
然后,使用循环遍历字符串中的每一个字符,判断该字符是否为目标字符。注意英文字符不区分大小写,可以使用`tolower`函数将字符转换为小写后进行比较。
如果当前字符与目标字符相同,就将目标字符的个数加1。
最后,输出统计结果,即目标字符在字符串中的个数。
以下是示例代码:
```c
#include <stdio.h>
#include <ctype.h> // 包含tolower函数
int main() {
char str[100];
char target;
int count = 0;
printf("请输入一个字符串:\n");
fgets(str, sizeof(str), stdin);
printf("请输入要统计的字符:\n");
scanf("%c", &target);
// 将目标字符转为小写
target = tolower(target);
for (int i = 0; str[i] != '\0'; i++) {
char current = tolower(str[i]);
if (current == target) {
count++;
}
}
printf("字符串中包含 %c 的个数为:%d\n", target, count);
return 0;
}
```
在示例代码中,首先定义了一个长度为100的字符数组`str`,用来存储用户输入的字符串。
然后,使用`fgets`函数获取用户输入的字符串,并将其存储在`str`中。
接着,使用`scanf`函数获取用户输入的要统计的字符,并将其存储在`target`中。
然后,使用`tolower`函数将`target`转换为小写。
接下来,使用循环遍历`str`中的每一个字符,在循环中使用`tolower`函数将当前字符转为小写,然后与`target`进行比较,如果相同,则将`count`加1。
最后,使用`printf`函数输出统计结果。
这样,就完成了统计字符串中某个字符的个数的任务。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)