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.pswp.cn/news/536234.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536234.shtml
英文地址,請注明出處:http://en.pswp.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、初…