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

LAB: C++/C common string functions part 2 reverse, reverse-new-strchr_ strrch, s

ID: 3885876 • Letter: L

Question

LAB: C++/C common string functions part 2 reverse, reverse-new-strchr_ strrch, strnca, 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 (charq reverse a string in ptace 7 nake a new string that 1s rever tind the first occurrence o tind the last occurrence of char reverse_ new_(const char) char strehr_ (char* s, char c): char strrchr (char* s. char c): . 11 concatenate up to n characters from t onto the end of s char strecat (char s, const chart, size t n): char, strstr (char s, const char t: find the first occurrence of string t in s ar* replace_ (char.s,char c. char d): replace all occurrences of c withd eplace all occurrences of substring t with new substring u r-(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 and needed. #ine luce #include lt Reverse string q in place char. reverse (char o // fill in code here Il Make a now string that is the reverse of string q-don't forget to fi11 in code here deleto@ it in main ll Find the first occurrence of c in s char strchr_(char s. char c): ril in code here Il Find the last occurrence of c in s rill in code here l Concatenate up to n characters of t onto s fill in code here Il Find the first occurrence of string t in s till in code here Il Replace all occurrences of c with d char strrchr_(char s. char c) char strncat_(char s, const char* t, size t n)I this will help you on replace str char* strstr (char s. const char* t) char replace (char s, char c, char d) / fill in code here ll Replace all occurrences of string t with string u Hint: make a temporary buffer that is the sane as s page 1 of 2 McCarthy

Explanation / Answer

char* reverse_(char *q) {
   char tmp;
   int i, l = strlen(q);
   for (i = 0; i < l/2; i++) {
       tmp = q[i];
       q[i] = q[l - 1 - i];
       q[l - 1 - i] = tmp;
   }
   return q;
}

char* reverse_new_(const char *q) {
   char* newString;
   int i, l = strlen(q);
   newString = char[strlen(q) + 1];
   for (i = 0; i < l; i++) {
       newString[i] = q[l - 1- 1];
   }
   newString[l] = q[l] ;
   return newString;
}

char* strchr_ (char* s, char c) {
   int i, l = strlen(q);
   for (i = 0; i < l; i++) {  
       if (s[i] == c)
           return s + i;
   return NULL;
}

char* strrchr_ (char* s, char c) {
   int i, l = strlen(q), index = -1;
   for (i = 0; i < l; i++) {  
       if (s[i] == c)
           index = i;
   if (index >= 0)
       return s + index;
   return NULL;
}

char* strcat_(char* s, shar* 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) {
   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;
}

Here you go champ :)