C 语言实例 - 查找字符在字符串中出现的次数

C 语言教程 · 2019-02-21 09:28:34

查找字符在字符串中的起始位置(索引值从 0 开始)。

实例

#include <stdio.h> int main() { char str[1000], ch; int i, frequency = 0; printf("输入字符串: "); fgets(str, (sizeof str / sizeof str[0]), stdin); printf("输入要查找的字符: "); scanf("%c",&ch); for(i = 0; str[i] != '\0'; ++i) { if(ch == str[i]) ++frequency; } printf("字符 %c 在字符串中出现的次数为 %d", ch, frequency); return 0; }

输出结果为:

输入字符串: codercto
输入要查找的字符: o
字符 o 在字符串中出现的次数为 2

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Effective Modern C++

Effective Modern C++

Scott Meyers / O'Reilly Media / 2014-12 / USD 49.99

Learn how to program expertly with C++ with this practical book from Scott Meyers, one of the world's foremost authorities on this systems programming language. Scott Meyers takes some of the most dif......一起来看看 《Effective Modern C++》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具