hello, i need help filling in the string functions below i did couple please if
ID: 3886211 • Letter: H
Question
hello, i need help filling in the string functions below
i did couple please if i made a mistake to fix them
LAB: C++/C common string functions part 2: reverse_, reverse_new, strchr_, strrchr, strncat, strstr replace, replace str Code and Algorithm Design This lab, based on K and R (aka: THE C PROGRAMMING LANGUAGE by Kernighan and Ritchie), is designed to give more practice in working with common C library functions used in C++/C (please refer to Chapter 5.5 when needed, or related sections (marked in the text). . char*reverse (char" q): .char* reverse_new_(const char* q): // reverse a string in place // make a new string that is reversed // find the first occurrence of c char* strchr_(char* s, char c); . char* strrchr(char* s, char c); find the last occurrence of c // concatenate up to n characters from t onto the end of s .char strncat_(char* s, const char* t, size t n): .char strstr_(char s, const char* t) / find the first occurrence of string t in s .char replace_(char s, char c, char d) / replace all occurrences of c with d . I replace all occurrences of substring t with new substring u char* replace_str_(char s, char* t, char u): All of these functions should be implemented with pointers. Students will also show each of the algorithms using spreadsheets (as in the previous lab). Don't forget to check: http://www.cplusplus.com and http:/len.cppreference.com when needed. strlen(const char s) en #include #include #include 3 p ro g r a mo st+ Il Reverse string q in place char reverse_(char* q) // fill in code here ll Make a new string that is the reverse of string q - don't forget to deletel it in main char reverse_new_(charq) ( /I fill in code here l Find the first occurrence of c in s strchr(char* char* s. char c) ; / fill in code here Find the last occurrence of c ins / fill in code here char strrchr (char* s, char c) ( Il Concatenate up to n characters of t ontos char" strncat-(char, s, const char. t, size-t n) { // this will help you on replace-str- ill in code here Il Find the first occurrence of string t in s fill in code here ll Replace all occurrences of c with d fill in code here Il Replace all occurrences of string t with string u char strstr_(char s, const char* t) t char* replace-(char* s, char c, char d) { Hint: make a temporary buffer that is the same as s McCarthy page 1 of 2Explanation / Answer
Here is the code for the first few functions:
#include <iostream>
#include <cstring>
#include <string>
char* reverse_(char* q) //reverse a string in place
{
char c;
char* end = q + strlen(q)-1;
while(q < end)
{
c = *q;
*q = *end;
*end = c;
++q;
--end;
}
return q;
}
char* reverse_new_(const char* q) //make a new string that is reversed
{
char* newstr = new char[strlen(q)+1];
int end = strlen(q) - 1;
int j = 0;
while(end >= 0)
{
*(newstr + j) = *(q + end);
end--;
j++;
}
*(newstr + j) = '';
return newstr;
}
char* strchr_(char* s, char c) //Find the first occurence of c.
{
int i = 0;
while(*(s+i) != '')
{
if(*(s+i) == c)
return s+i;
i++;
}
return NULL;
}
char* strrchr_(char* s, char c) //Find the last occurence of 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) //concatenate upto n characters from t onto the end of s.
{
int pos = strlen(s)-1;
while(n != 0 && *t != '')
{
*(s+pos) = *t;
n--;
pos++;
}
*(s+pos) = '';
return s;
}
char* strstr_(char* s, const char* t) //find the first occurence of string t in s
{
size_t sourceSize = strlen(s);
size_t targetSize = strlen(t);
int i;
for(i = 0; i <= sourceSize - targetSize; i++)
{
bool equals = true;
for(int j = 0; j < strlen(t); j++)
if(*(s+i+j) != *(t+j))
{
equals = false;
break;
}
if(equals)
return s+i;
}
return NULL;
}
If you need any refinements, just get back to me.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.