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
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
// 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 dont 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 } 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 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... " << "\tchar* reverse_(char* s) " << "\tchar* reverse_new_(char* s) " << "\tchar* strchr_(char* s, char c) " << "\tchar* strrchr_(char* s, char c) " << "\tchar* strstr_(char* s, const char* t) " << "\tchar* strncat(char* s, const char* t, size_t n) " << "\tchar* replace_(char* s, char c, char d) " << "\tchar* 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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started