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://www.codercto.com/courses/l/17.html

查看所有标签

Blog Design Solutions

Blog Design Solutions

Richard Rutter、Andy Budd、Simon Collison、Chris J Davis、Michael Heilemann、Phil Sherry、David Powers、John Oxton / friendsofED / 2006-2-16 / USD 39.99

Blogging has moved rapidly from being a craze to become a core feature of the Internetfrom individuals sharing their thoughts with the world via online diaries, through fans talking about their favori......一起来看看 《Blog Design Solutions》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具