/******************************************** * * * * * Lab 1 Out * * One dimensional arrays,pointers * ********************************************/ #include //Retrieves full name from the user and stores in the name array void readName( char *name, int max_length); //Returns the length of the string int getLength( char *name); //Returns the length (ending index) of the first name int getFirst( char *name); // Returns the starting index of the last word in a name int getLast( char *name); // Copies the contents of a source string to a destination string void copyString( char *dest, 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 name[75]; char first_name[25]; char last_name[25]; char yorN; const int max_length = 75; int length_first_name, start_last_name; do { readName(name,max_length); length_first_name = getFirst(name); start_last_name = getLast(name); copyString(last_name,&name[start_last_name]); copySubString(first_name,name,0,length_first_name); cout << "The full name is: " << name << endl; cout << "The first name is: " << first_name << endl; cout << "The last name is: " << last_name << endl; cout << "The number of words in the full name is: " << getWordCount(name) << endl; cout << "The length of the full name is: " << getLength(name) << endl; cout << "Would you like to enter another name? "; cin >> yorN; cin.ignore(); }while(yorN == 'y'); return 0; }//end of function main void readName(char *name, int max_length) { cout << "Please enter a full name with less than 75 characters: "; cin.getline(name,max_length); return; }//end of function readName int getLength(char *name) { int i; for(i=0; name[i] != NULL; ++i) { ; } return i; }//end of function getLength int getFirst(char *name) { int i; for(i=0; name[i] != ' '; ++i) { ; } return i; }//end of function getFirst int getLast(char *name) { int i; for(i=75; name[i] != ' '; --i) { ; } return (i+1); }//end of function getLast void copyString(char *dest, char *src) { for(int i=0; i < 25; ++i) { dest[i] = src[i]; } return; }//end of function copyString void copySubString(char *dst, char *src, int start_index, int end_index) { for(int i=start_index; i < end_index; ++i) { dst[i] = src[i]; } dst[i] = NULL; return; }//end of function copySubString int getWordCount(char *name) { int count=0; for(int i=0; name[i] != NULL; ++i) { if(name[i] == ' ') { count = count+1; } } return (count+1); }//end of function getWordCount