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

How would you create this using c++ language no global variables ? String and Fu

ID: 3818831 • Letter: H

Question


How would you create this using c++ language no global variables ? String and Function Practice Lab Problems 2/16 l. Write a C++ function with a string parameter, a character parameter, and an output file stream parameter. The function is to count the number of times the character is found in the string, and output the result to the file, with an appropriate label. 2. Write a Boolean function with two string parameters. The function returns true when the first string is a substring of the second string, otherwise it returns false. 3. Write a void function with a char parameter and a string parameter. The function removes every copy of the char value from the string value: the calling program receives the altered string.

Explanation / Answer

1:

void func1(string s, char ch, ofstream myfile){
   int n = std::count(s.begin(), s.end(), ch);
   myfile.open ("output.txt");
   myfile << "occurrence of character: " <<ch<< " in String: "<< s<<" is : "<< n;
   myfile.close();
}

---------------------------------------------

2.

bool func2(char str2[], char str1[]){
int l = 0; //Hold length of second string
  
//finding length of second string
for(l = 0; str2[l] != ''; l++);

int i, j;
  
for(i = 0, j = 0; str1[i] != '' && str2[j] != ''; i++)
{
if(str1[i] == str2[j])
{
j++;
}
else
{
j = 0;
}
}
return j == l;
}

----------------------------------------------------------------------------

3.

void func3(char str[], char ch) {
int i, j = 0;
int size;
char ch1;
char str1[10];

size = strlen(str);

for (i = 0; i < size; i++) {
if (str[i] != ch) {
ch1 = str[i];
str1[j] = ch1;
j++;
}
}
str1[j] = '';

cout << " corrected string is : "<< str1;
}

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