/**Write a function, char* strdup(const char*),
that copies a C-style string into memory it
allocates on the free store. Do not use any
standard library functions. Do not use subscripting;
use the dereference operator * instead.*/

#include <iostream>
using namespace std;

char* strdup(const char* pc) {
    ///const char* cp = pc;
    int n{0};
    while(*pc++) ++n;
    pc -= n+1;
    //cout << "\nThe length of your string is " << n;
    char* copystr = new char[n+1];
    for(int i = 0; i < n; pc++) {
        copystr[i] = *pc;
        cout << "\ncopystr[" << i << "]=" << copystr[i];
        ++i;
    }
    copystr[n]=0;
    return copystr;
}

int main() {
    char* str="hairball";
    //str[8]=0;
    char* nstr = strdup(str);
    cout << "\nnstr = " << nstr << endl;
    cout << "\nThe copy of hairball is " << nstr;
}
