Need help with the static 1d array and below ignore first tw

Need help with the static 1-d array and below ignore first two: here is the code i have so far

#include
#include
#include

using namespace std;

void get_string (char *mssg) {
   cout << \"Enter original string: \";
   cin.getline(mssg,255);
}

void get_search_replace (char *smssg,char *rmssg) {
   cout << \"Enter search string: \";
   cin.getline(smssg,255);

   cout << \"Enter replacement string: \";
   cin.getline(rmssg,255);
}

int search_replace (char*mssg,char*smssg,char*rmssg) {
   for (int k = 0;k < 255;k++){
       if (mssg[k] == smssg[0]){
           for (int i = 0;i < smssg.size();i++){
               if (mssg[k + i] != smssg [i])     
                   break;
               if (i == smssg.size() - 1)


           }
       }
   }

   }


int main () {
   int k = 0;
   char mssg [256];
   char rmssg [256];
char smssg [256];

   get_string (mssg);
   get_search_replace (smssg, rmssg);
   k = search_replace (mssg, smssg, rmssg);
   cout << \"Your new string is: \" << mssg << \".\" << endl;
   cout << \"Number of replacements: \" << k << \".\" << endl;

}

LAB #7 Each lab will begin with a brief demonstration by the TAs for the core concepts examined in this lab. As such, this document will not serve to tell you everything the TAs will in the demo. It is highly encouraged that you ask questions and take notes. In order to get credit for the lab, you need to be checked off by the end of lab. You can earn a maximum of 3 points for lab work completed outside of lab time, but you must finish the lab before the next lab. For extenuating circumstances, contact your lab TAs and Jennifer Parham-Mocello You may work with a partner or individually in the labs! (1 pts) Quiz/Survey in Class Section of Canvas You need to consent or not consent to be in the research study regarding your recitation designs. (3 pts) Understand Pointers/Dynamic Memory Write a function 3 different ways in C++ to create memory on the heap and assign it to a pointer outside the function. Remember, you can return an address or change what a pointer points to in a function by passing the pointer by reference or by passing the address of the pointer. What if you want to change the contents of what the pointer points to? Make a function that will set the contents of the space on the heap. Do you still need to pass by reference or the address of the pointer? Why or why not? How will you delete the memory off the heap? Try doing it outside a function, now inside a function. You can check to see if you have any memory leaks using valgrind %valgrind program-exe (3 pts) Static 1-d arrays: C-style Strings Change your implementation from lab #6 to use C-style strings, instead of C++ strings. A C-style string is a string of characters ended by the null character, \'\\0\'. Since you don\'t know how big the message will be, you need to declare a character array large enough to hold a sentence, usually 256 characters will do! This means that the string can hold a maximum of 255 characters, plus one null character. ® char mssg [256]; Create two more C-style strings for your search and replace strings too, but these can be smaller if you would like. Your getline0 call needs to change to cin.getline0, and this function will automatically insert a null character at the end of the characters read from

Solution

#include<iostream>
#include<string.h>

using namespace std;
/*read original string */
void get_string (char *mssg) {
cout << \"Enter original string: \";
cin.getline(mssg,255);
}

/*read search string as smssg and replacem as rmssg*/
void get_search_replace (char *smssg, char *rmssg)
{
cout << \"Enter search string: \";
cin.getline(smssg,255);
cout << \"Enter replacement string: \";
cin.getline(rmssg,255);
}


int search_replace (char *mssg, char *smssg, char *rmssg)
{

int num=0;
/* find lenght of smssg*/
int smssg_len=(unsigned)strlen(smssg);
int mssg_len=(unsigned)strlen(mssg);

for (int k = 0; k < mssg_len ; k++)
{

if (mssg[k] == smssg[0])
{
for (int i = 0; i < smssg_len; i++)
{
if (mssg[k + i] != smssg [i])
break;

if (i == smssg_len - 1)
{
for(i=0;i<smssg_len;i++)
{
mssg[k+i]=rmssg[i];
}
++num;

break;
}


}
}

}
return num;
}

int main () {
int k = 0;
char mssg [256];
char rmssg [256];
char smssg [256];
get_string (mssg);

get_search_replace (smssg, rmssg);
k = search_replace (mssg, smssg, rmssg);
cout << \"Your new string is: \" << mssg << \".\" << endl;
cout << \"Number of replacements: \" << k << \".\" << endl;
}

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

output sample 1:-

Enter original string: jajra
Enter search string: j
Enter replacement string: t
Your new string is: tatra.
Number of replacements: 2.

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

output sample 2:-

Enter original string: hellocheggll
Enter search string: ll
Enter replacement string: YY
Your new string is: heYYocheggYY.
Number of replacements: 2.

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

output sample 2:-

Enter original string: tyysqgfsadbncvadfhgjfbvsahdadjghliusfjsjjbad
Enter search string: ad
Enter replacement string: ZZ
Your new string is: tyysqgfsZZbncvZZfhgjfbvsahdZZjghliusfjsjjbZZ.
Number of replacements: 4.

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

If you have any query, please feel free to ask

Thanks a lot

Need help with the static 1-d array and below ignore first two: here is the code i have so far #include #include #include using namespace std; void get_string (
Need help with the static 1-d array and below ignore first two: here is the code i have so far #include #include #include using namespace std; void get_string (
Need help with the static 1-d array and below ignore first two: here is the code i have so far #include #include #include using namespace std; void get_string (

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site