M
M
Mykola Franchuk2016-08-19 21:08:25
C++ / C#
Mykola Franchuk, 2016-08-19 21:08:25

How to find the number of occurrences of characters in a string using extension methods?

We need to find the number of occurrences of characters from the char[] Splitters array in the input string using extension methods. Those. if:
input = "1010" then input.Count(...) = 0;
input = "10*10", then input.Count(...) = 1;
input = "10*10/", then input.Count(...) = 2;
and so on ...
Specifically, it is of interest that the number of occurrences be no more than one.
I thought to do it this way, but I haven’t worked with expanders yet and somehow it doesn’t work:

char[] Splitters = { ' ', '*', 'x', '/', ',', '&', '^' };
//...
bool ok = false;
for (int i = 0; i < Splitters.Length; i++){
if (input.Count(x => x == input.Contains(Splitters[i])) < 2)
                {
                    ok = true;
                }
                else
                    ok = false;
}

Contains returns bool, so it can't be used here. But what is right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Silin, 2016-08-19
@kuzia_bRatok

Enumerable.Count

#
#algooptimize #bottize, 2016-08-19
@user004

The task is almost solved. It remains to turn on the head.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question