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

Here are the exercises with the answers. All functions use a typical pointer solution. 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);

       //======================================================= myStrlen_p
           //=== return length of c-string (same as strlen)
       int  myStrlen_p(char* a) {
           char* cp = a;
           while (*cp++)
               ;
           return cp-a-1;
       }//end myStrlen_p

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

           void myStrcpy(char* a, char* b);

   Solution using pointers:

       //======================================================= myStrcpy_p
           //=== Like strcpy - typical cryptic coding.
       void myStrcpy_p(char* a, char* b) {
           while (*a++ = *b++)  // assignment, not comparison
               ;
       }//end myStrcpy_p