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

查看所有标签

Web Operations

Web Operations

John Allspaw、Jesse Robbins / O'Reilly Media / 2010-6-28 / USD 39.99

A web application involves many specialists, but it takes people in web ops to ensure that everything works together throughout an application's lifetime. It's the expertise you need when your start-u......一起来看看 《Web Operations》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换