////////////////////////////////////////////////////////////////// // strFunctions.cpp : Lab 8 // Today's date - 2/11/99 // // Name: Your name here // Class: C 647 Introduction to Programming with C++ (CHS) // Class: CP 107 Introduction to Programming with C++ (NHTI) ///////////////////////////////////////////////////////////////// // Description // Describe what you program is doing..... // ///////////////////////////////////////////////////////////////// // Program Output // output can be attached at the end of the program. // ///////////////////////////////////////////////////////////////// #include using namespace std; //#include //needed for string manipulation functions const char TAB = '\t'; void DisplayMenu(); int mystrcmp(char a[], char b[]); char * myStrcat(char *a, const char *b); void main() { const int STRING_LENGTH = 80; int iMenuItem = -1; int len ; // number of actual chars in the array char szDestString[STRING_LENGTH*2] = {0}; // source string char szSrcString[STRING_LENGTH] = {0}; //array to receive input chars char szFirstString[STRING_LENGTH], // arrays for receiving chars szSecondString[STRING_LENGTH] ; char *pTmpChr; // temporary pointer used for receiving a string while ( 1 ) { DisplayMenu(); iMenuItem = cin.get(); //need to flush enter key out of buffer cin.get(); // must do this to get the enter key switch(iMenuItem) { case '1': //strlen cout << endl << "Please enter a line of characters terminated with enter key" << endl ; cout << endl; cin.getline( szSrcString , STRING_LENGTH, '\n' ) ; // reads up to 79 chars or until //enter key detected or ctrlz cout << endl << "You just entered the following:" << endl ; cout << szSrcString << endl ; len = strlen ( szSrcString ) ; cout << "number of characters returned from strlen is " << len << endl ; break; case '2': // strcpy cout << endl << "Enter source string for strcpy test " << endl; cin.getline( szSrcString, STRING_LENGTH ) ; strcpy ( szDestString, szSrcString ); cout << endl << "destination string is " << endl << szDestString << endl; break; case '3': // strcmp cout << endl << "Enter first string for strcmp" << endl; cin.getline(szFirstString, STRING_LENGTH) ; cout << endl << "Enter second string for strcmp" << endl; cin.getline(szSecondString, STRING_LENGTH) ; int iReturnValue; //iReturnValue = strcmp ( szFirstString,szSecondString) ; iReturnValue = mystrcmp ( szFirstString,szSecondString) ; cout << endl << "iReturnValue is " << iReturnValue << endl ; if ( iReturnValue == 0 ) { cout << endl << "strings are equal" << endl ; } else if ( iReturnValue < 0 ) { cout << endl << "first string is less than second" << endl; } else { cout << endl << "first string is greater than second" << endl; } break; case '4': //strcat // Make sure that the first string has enough space // to hold the additional characters placed at the end cout << "Enter first string" << endl; cin.getline(szDestString, STRING_LENGTH); //cin >> szSrcString; // get the string that will be concatenated. cout << "Enter string that will be appended to the first string" << endl; cin.getline(szSrcString, STRING_LENGTH); pTmpChr = myStrcat(szDestString, szSrcString); cout << "New string after concatenation is "; cout << szDestString << endl ; cout << "New string after concatenation is using return value "; cout << pTmpChr << endl << endl << endl; break; case '5': default: cout << "BYE....." << endl; //cout << "You have entered " << iMenuItem << " which is not a valid number, enter [1 to 4]" << endl; return; }//end of switch case }//end of while loop } void DisplayMenu() { cout << TAB << "Please make the following menu choice" << endl; cout << TAB << TAB << "1. strlen example" << endl << TAB << TAB << "2. strcpy example" << endl ; cout << TAB << TAB << "3. strcmp example" << endl << TAB << TAB << "4. strcat example" << endl ; cout << TAB << TAB << "5. quit the program" << endl ; return; } int mystrlen(char a[]) { int len = 0 ; int i = 0; while (a[i] != '\0') { len++; i++; } return len; } void mystrcpy(char a[], char b[]) { int i = 0; while (b[i] != '\0') { a[i] = b[i]; i++ ; } a[i] = '\0' ; } int mystrcmp(char a[], char b[]) { int i = 0; int iRetVal = -1; while (a[i] != '\0' && b[i] != '\0') { if(a[i] < b[i]) return -1 ; if (a[i] > b[i] ) return 1; i++; } if (a[i] == b[i]) { iRetVal = 0; } else if(a[i] < b[i]) { iRetVal = -1 ; } else //(a[i] > b[i]) { iRetVal = 1; } return iRetVal; } ////////////////////////////////////////////////////////////////// // Function myStrcat ///////////////////////////////////////////////////////////////// // Description: // Take two strings arguments and concatenates // the second string at the end of the first string. ///////////////////////////////////////////////////////////////// // INPUTS: // char * szDestination string (where second string will be // concatenated. // char * String that will be attached to the first string // char * Same as first argument, concatenated string ///////////////////////////////////////////////////////////////// char * myStrcat(char *a, const char *b) { // save the starting location of the first // string. This is what we will return. char *retString = a; // go to the end of the first string while ( *a != '\0') a++; // we are using b + i notation just incase // the second argument was an array (constant // pointer). int i = 0; // used for traversing the string while ( *(b + i) != '\0') { *(a++) = *(b + i); // attach char from str b to str a i++ ; // point to next character } *a = '\0' ; // we pointer after the last char, add a null return retString; // return the concanated string. } ////////////////////////////////////////////////////////////////// // Function myStrcat ///////////////////////////////////////////////////////////////// // Description: // Take two strings arguments and concatenates // the second string at the end of the first string. ///////////////////////////////////////////////////////////////// // INPUTS: // char [] szDestination string (where second string will be // concatenated. // char [] String that will be attached to the first string // char * Same as first argument, concatenated string ///////////////////////////////////////////////////////////////// void myStrcatA(char a[], char b []) { // go to the end of the first array int i = 0; while ( a[i] != '\0') i++; // Start adding characters to the end // of the first string int j = 0; while ( b[j] != '\0') { a[i] = b[j]; j++; i++ ; } // Add a null character at the end of the array a[i] = '\0' ; }