c语言编写函数实现:统计字母的出现频度
时间: 2023-10-27 08:11:24 浏览: 106
好的,这个问题很简单,下面是一个 C 语言的函数实现,可以统计一个字符串中每个字母出现的频度:
```c
#include <stdio.h>
void count_letters(char string[]) {
int counts[26] = {0};
for (int i = 0; string[i] != '\0'; i++) {
if (string[i] >= 'a' && string[i] <= 'z') {
counts[string[i] - 'a']++;
}
else if (string[i] >= 'A' && string[i] <= 'Z') {
counts[string[i] - 'A']++;
}
}
for (int i = 0; i < 26; i++) {
if (counts[i] > 0) {
printf("%c: %d\n", 'a' + i, counts[i]);
}
}
}
int main() {
char string[] = "Hello, World!";
count_letters(string);
return 0;
}
```
以上是统计字母频度的 C 语言函数实现,希望对你有帮助。
相关问题
c语言编写函数实现:统计字母的出现频度并显示结果。(大小写算同一字母),且函数的形参为已经知道的数组
好的,这是一个计算字符串中字母出现频次的问题。以下是一个用C语言编写的函数:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void countLetters(char str[], int count[]) {
int len = strlen(str);
int i;
for(i = 0; i < len; i++) {
if(isalpha(str[i])) {
count[tolower(str[i]) - 'a']++;
}
}
}
int main() {
char str[100];
int count[26] = {0};
printf("请输入一个字符串:\n");
gets(str);
countLetters(str, count);
int i;
for(i = 0; i < 26; i++) {
if(count[i] > 0) {
printf("%c 出现的次数为 %d\n", 'a' + i, count[i]);
}
}
return 0;
}
```
你可以将上面的代码复制粘贴到你的 C 语言编译器中并运行它。如果你有任何问题,请联系我。
用c语言编写函数实现选择排序
下面是用 C 语言编写函数实现选择排序的代码:
```c
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n-1; i++) {
minIndex = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
```
这个函数接受一个整型数组和数组长度作为参数,会对数组进行选择排序,将数组元素从小到大排列。