题解 | 计算某字符出现次数
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
using System;
class Program {
public static void Main() {
string line;
int num = 0;
while ((line = System.Console.ReadLine ()) != null) { // 注意 while 处理多个 case
string tokens = line;
string t = Console.ReadLine();
if(t==null)continue;
//Console.WriteLine("第一行的字符串:{0},第二行是单个字符{1}",tokens[0],tokens[1]);
char c = t[0];
if(char.IsDigit(c)){
foreach(char ch in tokens){
if(ch == c)num++ ;
}
}
else{
c = char.ToLower(c);
foreach(char ch in tokens){
if(char.IsLetter(ch))
{
char ch1 = char.ToLower(ch);
if(ch1 == c)num++;
}
}
}
}
System.Console.WriteLine(num);
}
}
I don't know how to say.The model from the topic give me the programmer "while(line = Console.ReadLine() != EOF)",so I think it should be read the whole data include the second char into the "line".The first I use the method "split()" the line to tow parts,and save the one into token[0] and the other into token[1] ,the tokens type is string[],I am careless lead the result to false.
And the platform has a black heart,it has one situation will input a blank.So if you want to acess array like token[1] will cause error of illegal acess.So I add one judgement if the token.length is lower to 2.The process will execute continue and shutdown this one.
The grammar's question has been resloved.But the reading method has question.The "while" will read only a line,so I need to add a Console.ReadLine() in the "while" to read the second input.And the judgement I modify it to whether the second input is null or not to execute "continue".The problem has been satisfactorily resloved here.
