7-10 统计字符出现次数

import java.util.Scanner;

public class S7_10 {
	static String str;
	static char c;
	static int cnt;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		str = sc.nextLine();
		c = sc.nextLine().charAt(0);
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == c) {
				cnt++;
			}
		}
		System.out.println(cnt);
	}
}

#include <iostream>
#include<stdio.h>
#include<string.h>
#define N 85
using namespace std;
int main(void)
{
	int i, len = 0, cnt = 0;
	char ch;
	char str[N];   //存储输入的字符串
	char c;        //要查找的字符
	ch = getchar();  //输入字符串
	for (i = 0; ch != '\n'; i++){
		str[i] = ch;
		len++;
		ch = getchar();
	}
	scanf("%c",&c);   //输入要查找的字符
	for (i = 0; i < len; i++)  //查找字符并统计其出现次数
	{
		if (c == str[i])
		{
			cnt++;
		}
	}
	printf("%d\n", cnt);
	return 0;
}
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页