Question
Recall that a character (or char) represents things like a, Z and (that last one is a new line character). If you make an array
Recall that a character (or char) represents things like a, Z and (that last one is a new line character). If you make an array of characters ending with a \0, you have a text string. We will look at this soon. In the meantime, lets act like an array of chars is like any other array. Write a function replaceAllCh that takes 4 parameters: 1) a_chars, an array of characters that you will modify
2) numChars, the length of array a_chars 3) oldChar, a char to search for 4) newChar, a char to replace toFind with The function will return the number of old characters that are replaced by the new character. The function prototype will look like this: unsigned int replaceAllCh(char* a_chars, unsigned int numChars, char oldChar, char newChar); Example Calls: char a_chExample[] = { 'a', 'b', 'a', 'b', 'a', 'c', '\0' };
assert(replaceAllCh(a_chExample, 7, 'a', 'b') == 3); // The array is now {b, b, b, b, b, c, \0}
assert(replaceAllCh(a_chExample, 7, 'a', 'b') == 0); // T he array doesn t change. No a to remove assert(replaceAllCh(a_chExample, 7, 'b', '~') == 5); // The array is now {~, ~, ~, ~, ~, c, \0}
printf("The new character array is %s ", a_chExample);
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