Hello, I’m working on this task: Slightly modify the method AnalyzeString from the previous task. It should take two parameters, string input and ref int numberOfZeros. The method should add the number of zeros that it finds in input to numberOfZeros. From the Main method, read an integer count from the console. Then, read count strings from the console. Call AnalyzeString for every one of them. If all strings are empty, the output should be ‘All strings are invalid.’ - otherwise, output the total number of zeros in all strings.
Example 1:
4
I am y0ur father
H0ust0n, we have a pr0blem
Winter is c0mming
B0nd. James B0nd
7
Example 2:
2
All strings are invalid.
Here is my code:
using System;
namespace ParametersToMethods
{
class HowManyZer0s
{
static void Main()
{
int count = int.Parse(Console.ReadLine());
for (int i = 0; i <= count; i++)
{
var input = Console.ReadLine();
int numberOfZeros;
if (AnalyzeString(input, ref numberOfZeros))
Console.WriteLine(numberOfZeros);
else
Console.WriteLine("The string is invalid.");
}
}
static bool AnalyzeString(string input, ref int numberOfZeros)
{
numberOfZeros = 0;
if (string.IsNullOrEmpty(input))
return false;
for(int i = 0; i < input.Length; i++)
{
if (input[i] == '0')
numberOfZeros++;
}
return true;
}
}
}