C#统计字符出现的个数【C#】

C#统计字符出现的个数

题目描述

编写一个实例方法getCountChar方法。该方法参数有两个,第一个参数可以是字符串s,第二个参数为字符c,方法返回值为第二个参数在第一个参数中出现次数。例如,CountChar("6221982",'2')返回值为3。
部分程序代码已经给出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();     
            char ch = (char) Console.Read();
            Program pro = new Program();
            Console.WriteLine(pro.getCountChar(str,ch));

            //Console.ReadKey();
        }
        public int getCountChar(String s,char c){
        //


        在此处填写代码


        //
        }
    }
}

 

提交时,请提交整个题目!!!

输入

一个字符串s,一个字符c

输出

该字符c在字符串s中的出现次数

样例输入

6221982
2

样例输出

3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{class Program{static void Main(string[] args){string str = Console.ReadLine();char ch = (char)Console.Read();Program pro = new Program();Console.WriteLine(pro.getCountChar(str, ch));//Console.ReadKey();}public int getCountChar(String s, char c){int num = 0;for (int i = 0; i < s.Length; i++){if (s[i] == c) num++;}return num;}}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536234.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C# teacher类【C#】

C# teacher类 题目描述 定义一个教师类Teacher&#xff0c;具体要求如下&#xff1a; 1、私有字段工号no&#xff08;string&#xff09;、姓名name&#xff08;string&#xff09;、出生日期birthday&#xff08;DateTime&#xff09;、性别sex&#xff08;SexFlag&#xff0…

c#简单类的继承【C#】

c#简单类的继承 题目描述 编写代码实现&#xff1a;定义了三个类Bird、Mapie、Eagle。其中Bird为抽象类&#xff0c;定义了一个抽象方法Eat()。Mapie类和Eagle类为Bird的派生类。Mapie类中重写了Eat()方法&#xff0c;重载了一个Eat(int time)方法。Eagle类中也重写了Eat()方…

c#计算长方形的面积(继承问题)【C#】

c#计算长方形的面积&#xff08;继承问题&#xff09; 题目描述 根据给出的代码&#xff0c;补全缺失的代码&#xff0c;输入两个数字为长方形的长和宽&#xff0c;从而得出长方形的面积。 using System; namespace InheritanceApplication { class Shape { pub…

C#委托、类和事件的验证【C#】

C#委托、类和事件的验证 题目描述 程序由两部分组成&#xff0c;如下代码所示。第一部分定义了委托、类和事件。第二部分进行验证。 using System; namespace HelloWorldApplication { public delegate void DelegateRing();public class Bell{ public event DelegateRing R…

接口实例(C#,IShape)【C#】

接口实例&#xff08;C#,IShape&#xff09; 题目描述 接口实例。接口和类如下图所示&#xff0c;根据给出代码&#xff0c;补写缺失的代码&#xff0c;然后在Program类的静态Main方法中验证所实现的类。 using System; namespace Myinterface { public interface IShape…

c#补充print(多态性问题)【C#】

c#补充print&#xff08;多态性问题&#xff09; 题目描述 根据给出代码&#xff0c;补写缺失代码&#xff0c;当print函数内为整数的时候&#xff0c;输出整数的三次方&#xff0c;为浮点数&#xff0c;输出其二次方&#xff0c;为字符串时&#xff0c;直接输出。 using Sys…

1439: 2.4.5 Fractions to Decimals 分数化小数

1439: 2.4.5 Fractions to Decimals 分数化小数 时间限制: 1 Sec 内存限制: 64 MB提交: 194 解决: 13题目描述 写一个程序&#xff0c;输入一个形如N/D的分数(N是分子&#xff0c;D是分母)&#xff0c;输出它的小数形式。 如果小数有循环节的话&#xff0c;把循环节放在一对圆…

Problem B: 求各位数字之和

#include <stdio.h> #include <stdlib.h> int main() { int n,sum0,m; while(~scanf("%d",&n)) { while(n>0) { mn%10; nn/10; summ; } printf("%d\n",sum); sum0; } return 0; }

Problem C: 判断字符串是否为回文

#include <stdio.h> #include <stdlib.h> int main() { int i,j,n; char str[10]; gets(str); nstrlen(str); for(i0,jn-1;i<j;i,j--) { if(str[i]!str[j]) { printf("No\n"); break; } } if(i>j)printf("Yes\n"); return 0; }

Problem A: 童年生活二三事

斐波那契数列:F(n)F(n-1)F(n-2) #include <stdio.h> #include <stdlib.h> int f(int n) {int b;if(n1)b1;if(n2)b2;if(n>2)bf(n-1)f(n-2);return b; }int main() {int a,n;while(~scanf("%d",&n)&&n!0){af(n);printf("%d\n",a…

Problem C: 01字串

#include <stdio.h> #include <stdlib.h>int main() {int i,j,n0,m0;char a[129][8];for(i0;i<128;i){for(j0;j<7;j){a[i][j]n%2;nn/2;}m;nm;}for(i0;i<128;i){for(j6;j>0;j--)printf("%d",a[i][j]);printf("\n");}return 0; }

汉诺塔III

#include <stdio.h> #include <stdlib.h> int main() {int fx(int );int n,m;while(~scanf("%d",&n)){mfx(n);printf("%d\n",m);}return 0; } int fx(int n) {int m;if(n1)m2;if(n>1)m3*fx(n-1)2;return m; }

Problem B: C语言习题 学生成绩输入和输出

Problem B: C语言习题 学生成绩输入和输出 Description 编写一个函数print&#xff0c;打印一个学生的成绩数组&#xff0c;该数组中有5个学生的数据&#xff0c;每个学生的数据包括num(学号)、name(姓名)、score3。编写一个函数input&#xff0c;用来输入5个学生的数据。 I…

Problem E:结构体---点坐标结构体

Problem E: 结构体—点坐标结构体 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 585 Solved: 395 [Submit][Status][Web Board] Description 定义一个表示点坐标的结构体&#xff0c;输入两个点的坐标&#xff0c;输出这两个点中点的坐标 Input 第一个点的坐标&…

Problem F: 结构体--学生信息排序

Problem F: 结构体–学生信息排序 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 378 Solved: 192 [Submit][Status][Web Board] Description 定义存放一个学生信息的结构体类型&#xff0c;学生信息包括&#xff1a;姓名&#xff0c;学号&#xff0c;性别&#xff0c;…

Problem D: 分数减法——结构体

Problem D: 分数减法——结构体 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 604 Solved: 462 [Submit][Status][Web Board] Description 分数可以看成是由字符’/’分割两个整数构成&#xff0c;可以用结构体类型表示。请用结构体类型变量计算两个分数的差。 注意&…

Problem G: C语言习题 医生值班

Problem G: C语言习题 医生值班 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 847 Solved: 102[Submit][Status][Web Board]Description 医院内科有A,B,C,D,E,F,G,H共七位医生&#xff0c;每人在一周内要值一次夜班&#xff0c;排班的要求是&#xff1a; &#xff08;1&a…

Problem A: 删除区间内的元素(线性表)

Problem A: 删除区间内的元素&#xff08;线性表&#xff09; Time Limit: 1 Sec Memory Limit: 2 MBSubmit: 75 Solved: 24[Submit][Status][Web Board]Description 若一个线性表L采用顺序存储结构&#xff0c;其中元素都为整数。设计一个算法&#xff0c;删除元素值在[x,y…

问题 B: 调整表中元素顺序(线性表)

问题 B: 调整表中元素顺序&#xff08;线性表&#xff09; 时间限制: 1 Sec 内存限制: 2 MB提交: 28 解决: 11[提交][状态][讨论版]题目描述 若一个线性表L采用顺序存储结构存储&#xff0c;其中所有元素都为整数。设计一个算法&#xff0c;将所有小于0的元素移到所有大于0的…

Problem C: 顺序表基本运算(线性表)

问题 C: 顺序表基本运算&#xff08;线性表&#xff09; 时间限制: 1 Sec 内存限制: 128 MBDescription 编写一个程序&#xff0c;实现顺序表的各种基本运算&#xff08;假设顺序表的元素类型为char&#xff09;&#xff0c;主函数已给出&#xff0c;请补充每一种方法。 1、初…