Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Write a function replaceSingles(s, c) that takes a string s, and a character,

ID: 3728227 • Letter: 3

Question

3. Write a function replaceSingles(s, c) that takes a string s, and a character, c, and returns a copy of s with where every instance of c is replaced with a "*", UNLESS there are more than one of the characters in a row. Here are some example inputs/outputs:
replaceSingles("hello", "h") should return '*ello"
replaceSingles("hello", "l") should return 'hello'
replaceSingles('12345112121', '1') should return '*2345112*2*'
You only have to write the function, but you may want to write some test code to see if the function works. NOTE: replaceSingles does not print anything. 3. Write a function replaceSingles(s, c) that takes a string s, and a character, c, and returns a copy of s with where every instance of c is replaced with a "*", UNLESS there are more than one of the characters in a row. Here are some example inputs/outputs:
replaceSingles("hello", "h") should return '*ello"
replaceSingles("hello", "l") should return 'hello'
replaceSingles('12345112121', '1') should return '*2345112*2*'
You only have to write the function, but you may want to write some test code to see if the function works. NOTE: replaceSingles does not print anything. 3. Write a function replaceSingles(s, c) that takes a string s, and a character, c, and returns a copy of s with where every instance of c is replaced with a "*", UNLESS there are more than one of the characters in a row. Here are some example inputs/outputs:
replaceSingles("hello", "h") should return '*ello"
replaceSingles("hello", "l") should return 'hello'
replaceSingles('12345112121', '1') should return '*2345112*2*'
You only have to write the function, but you may want to write some test code to see if the function works. NOTE: replaceSingles does not print anything.

Explanation / Answer

code:


using namespace std;

char* replaceSingles(char s1[], char s2) {

int i, j, len;
//finds the length of the char array
for (i = 0; s1[i] != ''; i++) {
len++;

}
//performs the loop until all the characters are checked
for (i = 0; i <= len; ++i) {
if (s1[i] == s2) //if the first match found
{
j = i; //performs the loop until the no match is found
while (j <= len && (s1[j + 1] == s2)) {
j++;
}
if (j == i) // if i and j are equal then its a single occurance so replaces
s1[i] = '*';
else
i = j; //else changes the pointer because of continues match of characters

}


}

return (s1);
}

int main() {
char input1[] = "12345112121121";
char input2 = '1';
cout << "After replacing " << input1 << " with singles of " << input2 << " is :" << replaceSingles(input1, input2) << endl;
char t1[] = "hello";
char t2 = 'h';
char t3[] = "hello world";
char t4 = 'l';
cout << "After replacing " << t1 << " with singles of " << t2 << " is :" << replaceSingles(t1, t2) << endl;
cout << "After replacing " << t3 << " with singles of " << t4 << " is :" << replaceSingles(t3, t4) << endl;
return 0;
}

Sample Run:

After replacing 12345112121121 with singles of 1 is :*2345112*2112*
After replacing hello with singles of h is :*ello
After replacing hello world with singles of l is :hello wor*d

RUN SUCCESSFUL (total time: 206ms)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote