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

(C programing) looking for slution. Write a program that tests the following fun

ID: 3681559 • Letter: #

Question

(C programing) looking for slution.

Write a program that tests the following functions. Yes, you must implement the below functions without using any <string.h> library functions. You may use array and/or pointer notation to define the functions.

char *my_strcpy (char *destination, const char *source) - Copies all characters in the array pointed to by source into the array pointed to by destination. The null character is also copied. The function returns destination.

char *my_strcat (char *destination, const char *source) - This function appends a copy of the string pointed to by source (including the null character) to the end of the string pointed to by destination. The append overwrites the null character at the end of destination. The string pointed to by destination is returned.

int my_strcmp (const char *s1, const char *s2) - This function compares the string pointed to by s1 to the string pointed to by s2. If the string pointed to by s1 comes before the string pointed to by s2 in dictionary ordering, then -1 is returned. If the string pointed to by s1 is the same as the string pointed to by s2, then 0 is returned (the compare function is case sensitive). Otherwise 1 is returned.

int my_strlen (const char *s) - This function returns the length of the string pointed to by s. The computation of length does NOT include the null character.

Explanation / Answer

#ifndef MYCSTRING_H
#define MYCSTRING_H
#include <iostream>

using namespace std;

int my_strlen(const char* source)
{
   int len = 0;
   for(;source[len] != '';len++);
   return len;
}

char* my_strcpy (char* & destination, const char* source)
{
//   if(destination[0] == '') delete[] destination;
   int temp = my_strlen(source);
   destination = new char[temp];
   for(int i = 0; i < temp; i++)
       destination[i] = source[i];
   return destination;
}

char* my_strcat ( char* & destination, const char* source)
{
   if(!source) return destination;
   int temp = my_strlen(destination) + my_strlen(source) -1;
   char* tempc = new char[temp];

   for(int i = 0 ; i < my_strlen(destination) ; i++)
       tempc[i] = destination[i];
   int j = my_strlen(destination);

   for(int i= 0 ; i < my_strlen(source); i++,j++)
       tempc[j] = source[i];
   my_strcpy(destination,tempc);
   return destination;
}

int my_strcmp( const char* str1, const char* str2)
{
   for(int i = 0 ; str1[i] != '' && str2[i] != ''; i++)
   {
       if( str1[i] > str2[i]) return 1;
       if( str1[i] < str2[i]) return -1;
   }
   return 0;
}

char* my_strchr( char* str, int character)
{
   for(unsigned i = 0 ; i < my_strlen(str) ; i++)
   {
       int temp = str[i];
       if( temp == character) return str + i ;
   }
   return '';
}

char* my_strstr( char* str1, const char* str2)
{
   for(unsigned i = 0; i < my_strlen(str1); i++)
   {
       if(str1[i] == str2[i])
       {
           int a = my_strlen(str2);
           int k = i;
           bool stop = false;
           for(unsigned j = 0; j < my_strlen(str2); j++)
           {
               if(str1[k] != str2[j] )
               {
                   stop = true;
                   break;
               }
               k++;
           }

           if(!stop) return str1 + k - a ;  
       }
   }

   return NULL;
}
#endif


main.cpp
#include "myCString.h"
#include <iostream>

using namespace std;

int main()
{
   char* a = "hi";
   char* b = "bye";
   cout << " a contains :" << a << endl;
   cout << " a len :" << my_strlen(a) << endl;
   cout << " b len :" << my_strlen(b) << endl;

   char* c;
   my_strcpy(c,a);
   cout << "c contains :" << c << endl;
   my_strcpy(c,b);
   cout << "c now contains :" << c << endl;

   my_strcat(c,a);
   cout << "c is cat a : " << c << endl;
   my_strcat(c,b);
   cout << "c is cat b : " << c << endl;

   cout << "zero due to a being equal to a :" << my_strcmp(a,a);
   cout << " 1 if a < c :" << my_strcmp(a,c);
   cout << " -1 if a > c :" << my_strcmp(c,a) << endl;
    char* d = "dog goes out today";
   cout << " test string is: " << d << endl;
   char* pch = my_strchr(d,'e');
   cout <<" index num of 'e': " << pch - d + 1 << endl;
    pch = my_strchr(pch + 1 ,'s');
   cout <<" index num of 's': " << pch - d + 1 << endl;
   pch = my_strchr(pch+1, 'q');
   if(pch == '') cout << "CHAR NOT FOUND! " << endl;

   char str[] ="This is a simple string";
   char * pch2;
   pch2 = my_strstr (str,"simple");
    cout << pch2 << endl;
///   char* temp = my_strstr(str,"zz");
  
return 0;
}  

sample output

a contains :hi                                                                                                                                             
a len :2                                                                                                                                                   
b len :3                                                                                                                                                   
c contains :hi                                                                                                                                              
c now contains :bye                                                                                                                                         
c is cat a : byehi                                                                                                                                          
c is cat b : byehibye                                                                                                                                       
zero due to a being equal to a :0                                                                                                                           
1 if a < c :1                                                                                                                                              
-1 if a > c :-1                                                                                                                                            
                                                                                                                                                            
test string is: dog goes out today                                                                                                                          
index num of 'e': 7                                                                                                                                        
index num of 's': 8                                                                                                                                        
CHAR NOT FOUND!