Question
write a C program a) Write an int function countDigits(int x, int d) that counts the number of times that the digit d occurs in
write a C program
a) Write an int function countDigits(int x, int d) that counts the number of times that the digit d occurs in the integer x. That is, countDigits(12323, 2) would return 2 and countDigits(12323, 1) would return 1. You may assume that d is a number in the range 0..9 inclusive and that x is positive. countDigits(0,0) is 1. Hint: Be careful in coding when d is 0. countDigits(10,0) is 1.
(b) Write a unit test function testCountDigits(int x, int d, int result, char *msg) that calls countDigits(x,d) and compares the result with 'result', and prints out 'test passed' followed by the message msg if these values agree and 'test failed' followed by the message msg otherwise.
(c) Write a main function that executes your unit test functions and at lest calls
testCountDigits(123, 1, 1, "single digit test");
testCountDigits(10, 0, 1, "single 0 test");
testCountDigits(456456, 4, 2, "two fours");
testCountDigits(111111, 1, 6, "six 1's");
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started