I'd like to write OOP reusable code that does the following (just one solution):
- Count number of occurrences of specific letter in string: e.g. 'h' in "Hello"=>2
- Count number of occurrences of specific digit in a number e.g. 4 in 32456445 =>3
- Do the sum of two numbers: e.g. 5,4 =>9
If the required actions where Add & subtract, I can do this:
public delegate int BinaryOp(int x, int y);
public class SimpleMath
{
public static int Add(int x, int y) { return x + y; }
public static int Sub(int x, int y) { return x - y; }
}
Then do this for the Add (and something similar for Sub):
BinaryOp b = new BinaryOp(SimpleMath.Add);
My question is: what's the best solution for doing count specific letter in a string, count of specific digit in number, and Add(num1+num2) (best single OOP solution)?
Count('l',"Hello") => 2
Count(4,32456445 ) =>3
Add(5,4) =>9