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

C++, hello, can anyone take a look at my code and see why i am getting errors. /

ID: 3886480 • Letter: C

Question

C++, hello, can anyone take a look at my code and see why i am getting errors.

//

// main.cpp

// tuesday

//

// Created by macbook pro on 9/17/17.

// Copyright © 2017 macbook pro. All rights reserved.

//

#include <iostream>

#include <cstring>

#include <string>

char* reverse_(char*q){

  

char c;

char* end= q+ strlen(q)-1;

while(q<end){

  

c= *q, *q=*end, *end=c;

++q, --end;

  

  

}

  

return q;

}

char* reverse_new_(char* q) {

char* newstr= new char[strlen(q)+1];

char* end= newstr +strlen(q)-1;

while(*q !=''){

*end-- =*q++;

  

  

  

}

return newstr;

  

  

}

int strch_(char*s, char c){

  

int i = 0;

while(*(s+i) != '')

{

if(*(s+i) == c)

return c;

i++;

}

return NULL;

}

char* strrchr_(char*s, char c)

  

{

  

  

int i = 0;

char* pos = NULL;

while(*(s+i) != '')

{

if(*(s+i) == c)

pos = s+i;

i++;

}

return pos;

}

char* strncat_(char*s, const char*t, size_t n){

  

int i, j;

  

for(i = 0; s[i]!= ''; i++);

for(j = 0; t[j]!='' && j < n; j++, i++)

s[i] = t[j];

s[i] = '';

return s;

}

char* strstr_(char*s, const char* t){

  

  

int len_s, len_t;

for(len_s = 0; s[len_s]!=''; len_s++);

for(len_t = 0; t[len_t]!=''; len_t++);

  

for (int i = 0; i < (len_s-len_t); i++) {

if (s[i] == t[0]) {

int j;

for(j = 1; j < len_t; j++)

if(s[i+j] != t[j])

   break;

   if (j == len_t)

   return s+i;

}

  

}

return NULL;

}

  

char* replace_(char*s, char c, char d){

int i;

for(i = 0; s[i]!= ''; i++)

if(s[i] == c)

s[i] = d;

return s;

  

}

char *replace_str_ (const char *s, const char *t, const char *u)

{

  

char *ret;

  

int i, count = 0;

  

   int newlen = strlen(u);

  

int tlen = strlen(t);

  

for (i = 0; s[i] != ''; i++)

  

{

  

if (strstr(&s[i], t) == &s[i])

  

{

  

count++;

  

i += tlen - 1;

  

}

  

}

  

ret = (char *)malloc(i + count * (newlen - tlen));

  

if (ret == NULL)

  

exit(EXIT_FAILURE);

  

i = 0;

  

while (*s)

  

{

  

if (strstr(s, t) == s) //compare the substring with the newstring

  

{

  

strcpy(&ret[i], u);

  

i += newlen; //adding newlength to the new string

  

s += tlen;//adding the same t length the old string

  

}

  

else

  

ret[i++] = *s++;

  

}

  

ret[i] = '';

  

return ret;

}

void msgg_print (const std::string& msg, const std:: string s){

std::cout<< msg<< s<< std::endl;

  

}

#define BUF_SIZE 100

void msg_print(const std:: string&msg, const std:: string s){

  

std::cout<< msg<<s<<std::endl;

}

int main(){

  

  

  

std:: cout<< "LAB: (MORE ADVANCED) C++/C STRING FUNCTIONS TO IMPLEMENT... "

<< " char* reverse_(char*s) "

<<" char* reverse_new_(char*s) "

<<" char* strchr_(char*s, char c) "

<<" char* strrchr_(char*s, char c) "

<<" char* strstr_(char *s, const char*t) "

<< " char* strncat_(char*s, const char*t, size_t n) "

<< " char* replace_(char*s,char c, char d) "

<<" char* replace_str_(char*s, const char*t, size_t n) ";

  

char orig[BUF_SIZE]="indianapolis";

  

msg_print("before reversing...", orig);

reverse_new_(orig);

msg_print("and after..", orig);

reverse_(orig);

msg_print("and after reverse_ptr,,", orig);

  

  

msg_print(" find p...", strch_(orig, 'p'));

msg_print("find first i....", strch_(orig, 'i'));

msgg_print("find last i.....", strrchr_(orig, 'i');

strcpy(orig, "mississipi");

   std::cout<< "orig is:" <<orig<<" ";

   char*p=replace_(orig, 'i', 'X');

   std:: cout<<"after replacing i with X, orig is:"<< orig<<" ";

   strcpy(orig, "mississippi");

   std:: cout<<"norig is back to " <<orig<<" ";

   p=replace_str_(orig, "ss", "ketchup");

   std::cout<<"after replacing 'ss' with 'ketchup' orig is : "<<p<<" ";

   std:: cout<< "complete"<< std::endl;

Explanation / Answer

PROGRAM CODE:

#include <iostream>
#include <cstring>
#include <string>

char* reverse_(char*q){
  
char c;
char* end= q+ strlen(q)-1;
while(q<end){
  
c= *q, *q=*end, *end=c;
++q, --end;
  
  
}
  
return q;
}

char* reverse_new_(char* q) {

char* newstr= new char[strlen(q)+1];
char* end= newstr +strlen(q)-1;
while(*q !=''){
*end-- =*q++;
  
  
  
}
return newstr;
  
  
}

int strch_(char*s, char c){
  
int i = 0;
while(*(s+i) != '')
{
if(*(s+i) == c)
//changed here to return i and not c
return i;
i++;
}
return -1;

}

//shoudl this return char* or int?
char* strrchr_(char*s, char c)
  
{
  
  
int i = 0;
char* pos = NULL;
while(*(s+i) != '')
{
if(*(s+i) == c)
pos = s+i;
i++;
}
return pos;
}

char* strncat_(char*s, const char*t, size_t n){
  
int i, j;
  
for(i = 0; s[i]!= ''; i++);
for(j = 0; t[j]!='' && j < n; j++, i++)
s[i] = t[j];
s[i] = '';
return s;
}

char* strstr_(char*s, const char* t){
  
  
int len_s, len_t;
for(len_s = 0; s[len_s]!=''; len_s++);
for(len_t = 0; t[len_t]!=''; len_t++);
  
for (int i = 0; i < (len_s-len_t); i++) {
if (s[i] == t[0]) {
int j;
for(j = 1; j < len_t; j++)
if(s[i+j] != t[j])
break;
if (j == len_t)
return s+i;
}
  
}
return NULL;
}
  
char* replace_(char*s, char c, char d){
int i;
for(i = 0; s[i]!= ''; i++)
if(s[i] == c)
s[i] = d;
return s;
  
}

char *replace_str_ (const char *s, const char *t, const char *u)

{
  
char *ret;
  
int i, count = 0;
  
int newlen = strlen(u);
  
int tlen = strlen(t);
  
for (i = 0; s[i] != ''; i++)
  
{
  
if (strstr(&s[i], t) == &s[i])
  
{
  
count++;
  
i += tlen - 1;
  
}
  
}
  
ret = (char *)malloc(i + count * (newlen - tlen));
  
if (ret == NULL)
  
exit(EXIT_FAILURE);
  
i = 0;
  
while (*s)
  
{
  
if (strstr(s, t) == s) //compare the substring with the newstring
  
{
  
strcpy(&ret[i], u);
  
i += newlen; //adding newlength to the new string
  
s += tlen;//adding the same t length the old string
  
}
  
else
  
ret[i++] = *s++;
  
}
  
ret[i] = '';
  
return ret;
}


void msgg_print (const std::string& msg, const std:: string s){
std::cout<< msg<< s<< std::endl;
  
}

#define BUF_SIZE 100

// This function should have the second parameter as int
void msg_print(const std:: string&msg, int s){
  
std::cout<< msg<<s<<std::endl;
}

int main(){
  
  
  
std:: cout<< "LAB: (MORE ADVANCED) C++/C STRING FUNCTIONS TO IMPLEMENT... "
<< " char* reverse_(char*s) "
<<" char* reverse_new_(char*s) "
<<" char* strchr_(char*s, char c) "
<<" char* strrchr_(char*s, char c) "
<<" char* strstr_(char *s, const char*t) "
<< " char* strncat_(char*s, const char*t, size_t n) "
<< " char* replace_(char*s,char c, char d) "
<<" char* replace_str_(char*s, const char*t, size_t n) ";
  
char orig[BUF_SIZE]="indianapolis";
  
msgg_print("before reversing...", orig);
reverse_new_(orig);
msgg_print("and after..", orig);
reverse_(orig);
msgg_print("and after reverse_ptr,,", orig);
  
//changed the functon to match the argument
msg_print(" find p...", strch_(orig, 'p'));
msg_print("find first i....", strch_(orig, 'i'));
msgg_print("find last i.....", strrchr_(orig, 'i'));

strcpy(orig, "mississipi");
std::cout<< "orig is:" <<orig<<" ";

char*p=replace_(orig, 'i', 'X');
std:: cout<<"after replacing i with X, orig is:"<< orig<<" ";

strcpy(orig, "mississippi");
std:: cout<<"norig is back to " <<orig<<" ";

p=replace_str_(orig, "ss", "ketchup");
std::cout<<"after replacing 'ss' with 'ketchup' orig is : "<<p<<" ";


std:: cout<< "complete"<< std::endl;
}

OUTPUT:

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