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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!