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

I need help with some bugs I\'m getting on Xcode for this C++ code. \'strrev\' m

ID: 3886155 • Letter: I

Question

I need help with some bugs I'm getting on Xcode for this C++ code. 'strrev' may need to be 'strlen'. Also, I get a bug about converting a string literal to a string on C++ 11. Please help.

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

// Reverse string q in place
char* reverse_index_(char* q) {
strrev(q);
return q;
}
// Make a new string that is the reverse of string q – don’t forget to delete[] it in main
char* reverse_new_(char* q) {

int start,end1, len;
char temp, *ptr = NULL;

// find length of string
len = strlen(q);

// copy of string to ptr array
ptr = strcpy(ptr,q);

// swapping of the characters
for (start=0,end1=len-1; start<=end1; start++,end1--)
{
temp = ptr[start];
ptr[start] = ptr[end1];
ptr[end1] = temp;
}

// return pointer of reversed string
return ptr;
}
// Find the first occurrence of c in s
char* strchr_(char* s, char c){

int i, len ;
// calculating length of the string
len = strlen(s);
for( i=0 ; i<len ; i++){
if (*(s+i) == c)
{cout<<"The character first occurrence is found at "<<(i+1);
break;
}

}
return s;
}
// Find the last occurrence of c in s
char* strrchr_(char* s, char c) {
int i, len ;
// calculating length of the string
len = strlen(s);
for( i=len-1 ; i>=0 ; i--){
if (*(s+i)== c)
{cout<<"The character first occurrence is found at "<<(i+1);
break;
}

}
return s;
}
// Concatenate up to n characters of t onto s
char* strncat_(char* s, const char* t, size_t n) { // this will help you on replace_str_
// fill in code here
}
// Find the first occurrence of string t in s
char* strstr_(char* s, const char* t) {
// fill in code here
}
// Replace all occurrences of c with d
char* replace_(char* s, char c, char d) {
int i, len ;
// calculating length of the string
len = strlen(s);
for( i=0 ; i<len ; i++){
if (*(s+i)==c)
*(s+i)=d;
}

return s;
}
// Replace all occurrences of string t with string u
// Hint: make a temporary buffer that is the same as s
// use strstr_ to find the substring t (if any)
// then use strncat_ and strcat_ to copy the buffers across
// finally: delete[] the temporary buffer
char* replace_str_(char* s, char* t, char* u) {

char* temp;
temp = strstr(s,t);
s = strcat(s,u);

return s;
}
void msg_print(const std::string& msg, const std::string& s) {
std::cout << msg << s << std::endl;
}
#define BUF_SIZE 100


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 = "indianapolis";
msg_print("before reversing...", orig);
char* orignal = reverse_index_(orig);
msg_print("and after...", orignal);
char* orignal1 =reverse_new_(orig);
msg_print("and after reverse_ptr...", orignal1);
msg_print(" find p...", strchr_(orig, 'p'));
msg_print("find first i...", strchr_(orig, 'i'));
msg_print("find last i...", strrchr_(orig, 'i'));
strcpy(orig, "mississippi");
std::cout << " orig is now: " << orig << " ";
char* p = replace_(orig, 'i', 'X');
std::cout << "after replacing i with X, orig is: " << p << " ";
strcpy(orig, "mississippi");
std::cout << " orig is back to: " << orig << " ";
p = replace_str_(orig, "ss", "KETCHUP");
std::cout << "after replacing 'ss' with 'KETCHUP', orig is now: " << p << " ";
std::cout << " ...done ";
return 0;
}

Explanation / Answer

I have fixed all the code.

Since the question asks to implement all string functions, I am not sure if you are allowed to use strlen() library function in your code. so I have written our own version strlen_() which is used throughout. If you are allowed to use the library function itself, then you can replace the calls to the new function strlen_() with the library function strlen() itself.

Note: when you verify the results, please note that after usnig reverse_index_() , the original string is reversed inplace. so find p, first i and last i all be with reference to "silopanaidni"

Also allocating a string like this char* orig = "indianapolis"; and using it to store bigger length strings will lead to runtime errors. For example, in mississippi when ss is repaced with KETCHUP, the size of the array internall allocated to pointer *orig will not be enough. So it is better to declare orig with some big size like char orig[30]. So future expansions will have space in it.

Hope the answer helped. If it did, please don't forget to rate it . Thank you very much.

#include <iostream>
#include <cstring>
#include <string>
#define BUF_SIZE 100
using namespace std;

//find the length of string
int strlen_(const char *s)
{
int i = 0;
while(*(s+i) != '')
i++;
return i;
  
}
// Reverse string q in place
char* reverse_index_(char* q) {
int front = 0;
char temp;
int back = strlen_(q) - 1;
while(front < back)
{
temp = *(q + front);
*(q + front) = *(q + back);
*(q + back) = temp;
front++;
back--;
}
return q;
}
// Make a new string that is the reverse of string q – don’t forget to delete[] it in main
char* reverse_new_(char* q) {
int idx1, idx2 , len;
char *ptr = NULL;
// find length of string
len = strlen_(q);
// copy of string to ptr array
ptr = new char[len + 1]; //1 extra for
ptr[len] = ''; //store terminating char
  
for(idx1 = 0, idx2 = len-1; idx1 != len; idx1++, idx2--)
*(ptr + idx1) = *(q + idx2);
  
return ptr;
}

// Find the first occurrence of c in s
char* strchr_(char* s, char c){
int i, len ;
// calculating length of the string
len = strlen_(s);
for( i=0 ; i < len ; i++){
if (*(s+i) == c)
{
  
return s+i;
  
}
}
return 0; //when not found
}
// Find the last occurrence of c in s
char* strrchr_(char* s, char c) {
int i, len ;
// calculating length of the string
len = strlen_(s);
for( i=len-1 ; i>=0 ; i--){
if (*(s+i)== c)
{
return s+i;
}
}
return 0; //when not found
}

// Concatenate up to n characters of t onto s
char* strncat_(char* s, const char* t, size_t n) { // this will help you on replace_str_
  
// fill in code here
int i , j ;
int len = strlen_(s);
//start copying after end of s
for(i = 0, j = len; i < n; i++, j++)
{
*(s+j) = *t;
if(*t == '') //already reached end of string
break;
}
  
*(s+j) = ''; //put the terminating
  
return s;
  
}
// Find the first occurrence of string t in s
char* strstr_(char* s, const char* t) {
// fill in code here
int i, j, k;
int len1 = strlen_(s);
int len2 = strlen_(t);
bool found ;
for(i = 0; i <= len1 - len2; i++)
{
found = true; //assume found
//start from ith location of s and compare
for(j = 0, k = i; j < len2; j++, k++)
{
if(*(s+k) != *(t+j)) //if any of the characters dont match, stop going further
found = false;
break;
}
if(found)
return s+i;
}
return 0; //not found
}
// Replace all occurrences of c with d
char* replace_(char* s, char c, char d) {
int i, len ;
// calculating length of the string
len = strlen_(s);
for( i=0 ; i<len ; i++){
if (*(s+i)==c)
*(s+i)=d;
}
return s;
}
// Replace all occurrences of string t with string u
// Hint: make a temporary buffer that is the same as s
// use strstr_ to find the substring t (if any)
// then use strncat_ and strcat_ to copy the buffers across
// finally: delete[] the temporary buffer
char* replace_str_(char* s, char* t, char* u) {
char* newstr = new char[BUF_SIZE];
char* ptr = s;
char* prev = s;
int ulen = strlen_(u);
int tlen = strlen_(t);
int toIndex = 0;
int fromIndex = 0;
int n;
*newstr = '';
while((ptr = strstr_(s + fromIndex, t)) != 0)
{
n = ptr - (s + fromIndex);
if(n > 0)
{
strncat(newstr + toIndex, s + fromIndex , n);
toIndex += n;
fromIndex += n;
}
  
strncat(newstr + toIndex, u, ulen);
toIndex += ulen; //skip by length of u (the replacement length)
fromIndex += tlen; //skip by length of t (string to be replaced)
}
  
//copy any remaining portion
n = strlen_(s + fromIndex);
if(n > 0)
strncat(newstr + toIndex, s + fromIndex , n);
*s = '';
strncat(s, newstr, strlen_(newstr));
delete[] newstr;
return s;
}
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[50] = "indianapolis";
msg_print("before reversing...", orig);
char* orignal = reverse_index_(orig);
msg_print("and after...", orignal);
char* orignal1 =reverse_new_(orig);
msg_print("and after reverse_ptr...", orignal1);
  
msg_print(" find p...", strchr_(orig, 'p'));
msg_print(" find first i...", strchr_(orig, 'i'));
msg_print(" find last i...", strrchr_(orig, 'i'));
strcpy(orig, "mississippi");
std::cout << " orig is now: " << orig << " ";
char* p = replace_(orig, 'i', 'X');
std::cout << "after replacing i with X, orig is: " << p << " ";
strcpy(orig, "mississippi");
std::cout << " orig is back to: " << orig << " ";
p = replace_str_(orig, "ss", "KETCHUP");
std::cout << "after replacing 'ss' with 'KETCHUP', orig is now: " << p << " ";
std::cout << " ...done ";
  
delete[] orignal1; //deallocate
return 0;
}

output


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)
before reversing...indianapolis
and after...silopanaidni
and after reverse_ptr...indianapolis
find p...panaidni
find first i...ilopanaidni
find last i...i
orig is now: mississippi
after replacing i with X, orig is: mXssXssXppX
orig is back to: mississippi
after replacing 'ss' with 'KETCHUP', orig is now: miKETCHUPiKETCHUPippi