/******************************************** * * * * * Lab 2 Out * * Dynamic Memory Allocation of 1-D Arrays * ********************************************/ #include //Returns the full name as dynamically allocated char* char* readName(); //Returns the length of the string int getLength( char* name); //Returns the first word of a name (first name) as a dynamically allocated char * char* getFirst( char* name); //Returns the last word of a name (last name) as a dynamically allocated char* char* getLast( char* name); // Copies the contents of a source string to a destination string void copyString( char* dst, char* src); // Copies a substring with a specified starting index and a specified ending // index from a source string to a destination string. void copySubString( char* dst, char* src, int start_index, int end_index); // Returns the number of words in a string (Count the number of spaces + 1) int getWordCount( char* name); using namespace std; int main(void) { char* full_name = NULL; char* first_name = NULL; char* last_name = NULL; char yorN; int max_length = 0; do { //Get the name from the user full_name = readName(); //Get only the first name first_name = getFirst(full_name); //Get only the last name last_name = getLast(full_name); //Display the data cout << "The full name is: " << full_name << endl; cout << "The first name is: " << first_name << endl; cout << "The last name is: " << last_name << endl << endl; cout << "Would you like to enter another name? "; cin >> yorN; cin.ignore(); }while(yorN == 'y'); return 0; }//end of function main char* readName() { char buffer[255]; char* name; //Prompt user for full name cout << "Please enter a full name: "; cin.getline(buffer,sizeof(buffer)); //Dynamically allocate an array name = new char[strlen(buffer) + 1]; //Copy name from buffer to name strcpy(name, buffer); return (name); }//end of function readName int getLength(char* full_name) { int i; //Run a loop to count the length of the name for(i=0; full_name[i] != NULL; ++i) { ; } return i; }//end of function getLength char* getFirst(char* full_name) { char* first_temp = NULL; int i; //Loop to get the length of the first name for(i=0; full_name[i] != ' '; ++i) { ; } //Dynamically allocate an array first_temp = new char[i + 1]; //Copy the first name into the array first_temp copySubString(first_temp,full_name,0,i); return (first_temp); }//end of function getFirst char* getLast(char* full_name) { int i = 0; int str_length; char* last_temp = NULL; str_length = getLength(full_name); //Loop to get the starting address of the last name for(i=str_length; full_name[i] != ' '; --i) { ; } //Dynamically allocate an array last_temp = new char[str_length - (i + 1)]; //Copy the last name in the array last_temp copyString(last_temp,&full_name[i+1]); return (last_temp); }//end of function getLast void copyString(char* dst, char* src) { int i; //Copy the contents of src to dst one character at a time for(i=0; i < (getLength(src)); ++i) { dst[i] = src[i]; } //Put a Null on the end dst[i] = NULL; return; }//end of function copyString void copySubString(char *dst, char *src, int start_index, int end_index) { //Copy the contents of src to dst one character at a time for(int i=start_index; i < end_index; ++i) { dst[i] = src[i]; } //Put a NULL on the end dst[i] = NULL; return; }//end of function copySubString