/*
1. 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;

#define MAXLENGTH 10000

char* strdup01(const char* cstr) {
	cout << "input is " << cstr << '\n';
	int k = 137;
	int* pi = &k;
	cout << "pi = " << pi << '\n';

	char* cstrCopy = new char[MAXLENGTH];
	int i{ 0 };
	while (*cstr) {
		*(cstrCopy+i) = *cstr;
		//*cstrCopy = *cstr;
		cstr++;
		//cstrCopy++;
		i++;
	}
	//cstrCopy[i] = '\0';
	*(cstrCopy+i) = '\0';
	//cstrCopy -= i;
	return cstrCopy;
}

int main() {
	cout << "Enter a string: ";
	char* cstr = new char[MAXLENGTH];
	char* cstrCopy = new char[MAXLENGTH];
	while (cin.getline(cstr, MAXLENGTH)) {
		cstrCopy = strdup01(cstr);
		cout << cstrCopy << '\n';
		cout << "Enter a string: ";
	}
	delete[] cstr;
	delete[] cstrCopy;
}