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

Solve the following questions using c++ program. provide enough detail and comme

ID: 3822932 • Letter: S

Question

Solve the following questions using c++ program. provide enough detail and comments. (use compiler Xcode or visual basic 2012 or above)

1) Implement a function named reverse which takes a string and reverses it. The function returns nothing. Provide an example of client code calling the function.

2) Implement a function named replaceAll which takes a string str and two characters oldChar and newChar as parameters. It replaces all occurrences of oldChar in the string with newChar. The function returns nothing. Provide an example of client code calling the function.

Explanation / Answer

#include <iostream>
using namespace std;

// Function to reverse a string
void reverse(string &str)
{
int n = str.length();

// Swap character starting from two
// corners
for (int i=0; i<n/2; i++)
{
char tmp = str[i];
str[i] = str[n-i-1];
str[n-i-1] = tmp;
}
}

void replaceAll(string &str, char oldChar, char newChar)
{
int n = str.length();

// Swap character starting from two
// corners
for (int i=0; i<n; i++)
{
if (str[i] == oldChar)
{
str[i] = newChar;
}
}
}

// Driver program
int main()
{
string str = "clientstring";
cout << "Original string: " << str << endl;
reverse(str);
cout << "Reversed string: " << str << endl;

cout << "Original string: " << str << endl;
replaceAll(str, 'i', 'o');
cout << "String after replaceAll i with o: " << str << endl;
cout << str;

return 0;
}

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