🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# C 程序:查找字符串中字符的频率 > 原文: [https://www.programiz.com/c-programming/examples/frequency-character](https://www.programiz.com/c-programming/examples/frequency-character) #### 在此示例中,您将学习查找字符串中字符的频率。 要理解此示例,您应该了解以下 [C 编程](/c-programming "C tutorial")主题: * [C 数组](/c-programming/c-arrays) * [C 编程字符串](/c-programming/c-strings) * * * ## 找出字符的频率 ```c #include <stdio.h> int main() { char str[1000], ch; int count = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); printf("Enter a character to find its frequency: "); scanf("%c", &ch); for (int i = 0; str[i] != '\0'; ++i) { if (ch == str[i]) ++count; } printf("Frequency of %c = %d", ch, count); return 0; } ``` **输出** ```c Enter a string: This website is awesome. Enter a character to find its frequency: e Frequency of e = 4 ``` * * * 在该程序中,用户输入的字符串存储在`str`中。 然后,要求用户输入要找到其频率的字符。 它存储在变量`ch`中。 然后,使用`for`循环迭代字符串的字符。 在每次迭代中,如果字符串中的字符等于`ch`,则`count`增加 1。 最后,打印存储在`count`变量中的频率。