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

Problem 2 Input a line of text in a character array line of size 80. Input two c

ID: 3831179 • Letter: P

Question

Problem 2 Input a line of text in a character array line of size 80. Input two characters ch1 and ch2. Display the count of ch1 in line and replace all the occurances of ch1 with ch2 and display the modifed line. Enter a line of text They went to the 200 to sleep 2222 A Enter a character to replace 2 Enter a character to replace z with: 2 Count of 2 5 to sleep 2222 New line They went to the 2oo Enter a line of text A Quick Brown Funny Frozen Fox Flies and Flies Enter a character to replace F Enter a character to replace F with: C Count of F: 5 New line A Quick Brown Cunny Crozen Cox Clies and Clies.

Explanation / Answer

Here is your code: -

#include<iostream>
#include<cstring>

using namespace std;

int main() {
   cout<<"Enter a line of text:";
   char str[80];
   cin.get(str, 80); // getting whole line and saving it in str array
   cout<<"Enter a character to replace:";
   char c;
   cin>>c;
   cout<<"Enter a character to replace "<<c<<" with:";
   char r;
   cin>>r;
  
   int len = strlen(str); // getting length of string
   int count = 0;
   for(int i = 0; i<len;i++) {
       if(str[i] == c) { // checking if character matches in string then increase the count and replace it with the other character
           count++;
           str[i] = r;
       }
   }
  
   cout<<endl;
   cout<<"Count of "<<c<<" :"<<count<<endl;// printing count
   cout<<"New Line :";
   for(int i = 0; i<len;i++) {//printing new line
       cout<<str[i];
   }
   return 0;
}

Sample Output: -

Enter a line of text:I dont know where you are and what you are , but I'll kill
you
Enter a character to replace:w
Enter a character to replace w with:L

Count of w :3
New Line :I dont knoL Lhere you are and Lhat you are , but I'll kill you
--------------------------------
Process exited after 25.04 seconds with return value 0
Press any key to continue . . .

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