# C++: C-Strings: Exercises 1: Array Answers

Here are the exercises with the answers. All problems are solved in the simplest array-based style The prototypes for the actual library functions are slightly different, especially because some of the library functions return the address of the first argument instead of void.

1. Write a `myStrlen` function which is the same as the library `strlen` function. Assume the prototype is

           int myStrlen(char a[]);

   Solution using arrays:

       //========================================================= myStrlen
           //=== return length of c-string (same as strlen)
       int  myStrlen(char a[]) {
           int len = 0;
           while (a[len] != '\0') {
               len++;
           }
           return len;
       }//end myStrlen

2. Write the `myStrcpy` function which is the same as the library `strlen` function. Assume the prototype is

           void myStrcpy(char a[], char b[]);

       //========================================================= myStrcpy
           //=== copies second arg to first (like strcpy)
       void myStrcpy(char a[], char b[]) {
           // copy b to a .  Could use do-while.
           int i;  // declared outside loop so can be used later
           for (i = 0; b[i] != '\0'; i++) {
               a[i] = b[i];
           }
           a[i] = '\0';  // terminate the string
       }//end myStrcpy

3. Write the `myStrcat` function which is the same as the library `strlen` function. Assume the prototype is

           void myStrcat(char a[], char b[]);

   Solution:

       //========================================================= myStrcat
           //=== copy second arg to end of first (like strcat)
       void myStrcat(char a[], char b[]) {
           int to = 0;  // where to copy the second c-string to.
           // find the end of the first string.
           while (a[to] != '\0') {
               to++;
           }
           // The preceding loop left "to" at a's terminating 0.
           // copy b onto end of a, then add terminating 0.
           int from = 0;  // index of char in b
           while (b[from] != '\0') {
               a[to] = b[from];
               to++;
               from++;
           }
           a[to] = '\0'; // copy final zero

       }//end myStrcat

4. Write the `myStrcmp` function which is the same as the library `strlen` function. Assume the prototype is

           int myStrcmp(char a[], char b[]);  // array version

   For example,

                i = myStrcmp("abc", "after");  // result is a negative value

   There is a slight twist to this problem. The standard allows each compiler to decide if the `char` type is signed or not, so the range of of a `char` is either -128..127 (signed) or 0..255 (unsigned). To do the comparison properly, the comparison should be done on the unsigned characters, which requires casting to that type.
   Solution:

       //======================================================== myStrcmp
           //=== Same as library strcmp:,
           //< 0 means a < b, == 0 if a == b, > 0 if  a > b
       int  myStrcmp(char a[], char b[]) {
           int i;
           for (i=0; a[i]==b[i]; i++) {
               if (a[i] == '\0') {
                   return 0;  // they're equal
               }
           }
           // Because characters may be signed, the following casts
           // are necessary for codes beyond ascii [ie, 0x7F],
           // which will otherwise be negative.
           return (unsigned char)a[i] - (unsigned char)b[i];
       }//end myStrcmp