Home > database >  Please answer
Please answer

Time:09-18

Known a string s, design an algorithm to count the number of occurrences of a character in the string s

CodePudding user response:

 using System; 
using System.Linq;

The namespace ConsoleApp16
{
Class Program
{
The static void Main (string [] args)
{
String STR="123456781 abcdef2";
Char c='1';

Int result=STR. ToCharArray (). The Select (p=& gt; P==c). The Count (p=& gt; P);
In the Console. WriteLine (" {0} {1} {2} "the number is, STR, c, result);
Console.ReadLine();
}
}
}



And this is the c # way,

 DECLARE @ STR NVARCHAR (MAX), @ c NCHAR (1) 
The SET @ STR='123456781 abcdef2';
SET @ c='1'

SELECT COUNT (1) AS the result
The FROM (
SELECT SUBSTRING (@ STR, sv. Number, 1) AS c
FROM the MASTER. The dbo. Spt_values AS sv
WHERE the sv. [type]='P AND sv. Number> 0
) AS tt
WHERE tt. C=@ c
/*
The result
2
*/

This is essentially a wording,

CodePudding user response:

Can be written in C
  • Related