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

C++ The function should take as input an array of characters, a character, a sec

ID: 642166 • Letter: C

Question

C++

The function should take as input an array of characters, a character, a second character, and an int. It should return nothing. The 4th parameter is the length of the input array. The function should change the array so that every time the first parameter character occurs in the array, it should be changed to the second parameter character. Now in a separate file create your main function. Make sure you include at the top a function declaration for Problem4. Inside main create an array of characters (note that this is not a string, it is an array of characters) . Print out the array of characters (if you want to write a separate function for printing out an array of characters, that is fine). Call the function Problem4, with the array you created, along with 2 characters and the length of the array. After the call to the function, print out the array again. It should have changed

Explanation / Answer

#include <iostream>
using namespace std;
void function(char array[],char firstChar,char secondChar,int len);
void printCharArray(char array[]);
int main()
{

   char array[250];
   char firstChar,secondChar;
   int len=0,i=0;

   cout << "Enter a string :" << endl;
   cin>>array;

   //Calculate length of array
   while(array[i]!='')
   {

       i++;
       len++;
   }

   cout<<"Enter first character : ";
   cin>>firstChar;
  
   cout<<"Enter second character : ";
   cin>>secondChar;
  
   cout<<"Before Function call , Array content : ";
   printCharArray(array);
   function(array,firstChar,secondChar,len);
  
   cout<<" After Function call , Array content : ";

   printCharArray(array);
   cout<<endl;
  
   return 0;
}

void function(char array[],char firstChar,char secondChar,int len)
{

   for(int i=0;i<len;i++)
   {
       if(array[i]==firstChar)
       {
           array[i]=secondChar;
       }
   }
}

void printCharArray(char array[])
{

   cout<<array;
}

=====================================

Output

=====================================

sh-4.3# g++ -o main *.cpp                                                                                                                                                

sh-4.3# main                                                                                                                                                             

Enter a string :                                                                                                                                                         

programming                                                                                                                                                              

Enter first character : g                                                                                                                                                

Enter second character : k                                                                                                                                               

Before Function call , Array content :                                                                                                                                   

programming                                                                                                                                                              

After Function call , Array content :                                                                                                                                    

prokrammink                                                                                                                                                              

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