Descripción del Título
Escriba un método de instancia getCountChar. Este método tiene dos parámetros: el primer parámetro puede ser la cadena s, el segundo parámetro es el carácter c, y el valor de retorno del método es el número de veces que el segundo parámetro aparece en el primer parámetro. Por ejemplo, CountChar ("6221982", '2') devuelve un valor de 3.
Se ha proporcionado parte del código del programa.
usando System;
usando System.Collections.Generic;
usando System.Linq;
usando System.Text;
usando System.Threading.Tasks;
espacio de nombres 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) { // Complete el código aquí // } } }
Al enviar, envíe la pregunta completa. ! !
ingresar
Una cadena s, un carácter c
Producción
El número de apariciones del carácter c en la cadena s
Entrada de muestra
6221982 2
Salida de muestra
3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
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 count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == c)
{
count++;
}
}
return count;
}
}
}