For school i had to make an assignment, which i handed in already, but the code i wrote is awful, i don't like what i ended up with. So, I'm curious, what would be considered the best possible way to solve the following question in C#:
'//4 How many times does “queen” occur in the Alice in Wonderland book? Write some code to count them.'
link to the book (pastebin): book
my code (pastebin): my code (ugly)
please when writing your answer, ignore my code. also, explain what your code does, and why you think it's the best possible solution. The amount of times the word "queen" occurs in the book should be 76.
I won't post the full code, as I think it is useful for you to try this as an exercise, but I would personally go for a solution with the IndexOf
overload that takes a starting position.
So something like (note: intentionally incorrect):
int startingPosition = 0;
int numberOfOccurrences = 0;
do {
startingPosition = fullText.IndexOf("queen", startingPosition);
numberOfOccurrences++;
} while( matchFound );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With