/* * File: strtest.cpp * Author: Robert I. Pitts * Last Modified: November 10, 1999 * Topic: Manipulating Strings * ---------------------------------------------------------------- * * This program demonstrates some programmer-defined string functions. * */ #include /************************ Function Prototypes ************************/ /* * Function: StrLength * Usage: n = StrLength(str); * * Returns the length of a string, i.e., the number of characters in it, * not including the nul character (\0). */ int StrLength(char *str); /* * Function: StrCompare * Usage: StrCompare(str1, str2); * * Compares two strings indicating their lexical order as follows: * * If str1 < str2 returns a negative value. * If str1 > str2 returns a positive value. * If str1 == str2 returns zero. * * Upper- and lowercase letters are treated as being different. */ int StrCompare(char *str1, char *str2); /* * Function: StrCopy * Usage: StrCopy(dest, source) or dest_ptr = StrCopy(dest, source); * * Copies the contents (i.e., the characters) of the 'source' string * into the 'dest' string. The 'dest' string must contain enough space * to hold the source string otherwise the copy operation will try to * overwrite memory beyond the 'dest' string. The 'dest' is returned. */ char *StrCopy(char *dest, char *source); /* * Function: StrConcat * Usage: StrConcat(dest, source); or dest_ptr = StrConcat(dest, source); * * Appends the contents (i.e., the characters) of the 'source' string to * the end of the 'dest' string. The 'dest' string must contain enough * space to hold these additional characters otherwise the concatenation * will try to overwrite memory beyond the 'dest' string. The 'dest' * is returned. */ char *StrConcat(char *dest, char *source); /*************************** Main Program **************************/ int main() { char str1[80]; // An array to hold a string. char *str_ptr = str1; // A pointer that refers to that array. char str2[80]; // Another array to hold a string. cout << "Enter 1st string (no spaces): "; cin >> str1; cout << "Enter 2nd string (no spaces): "; cin >> str2; cout << endl; cout << "\"" << str1 << "\" is of length " << StrLength(str1) << endl; cout << "\"" << str2 << "\" is of length " << StrLength(str2) << endl; cout << endl; int cmp = StrCompare(str_ptr, str2); // Compare str1 and str2 (use pointer // str_ptr to refer to str1). if (cmp > 0) cout << "\"" << str1 << "\" is greater than \"" << str2 << "\"" << endl; else if (cmp < 0) cout << "\"" << str1 << "\" is less than \"" << str2 << "\"" << endl; else cout << "\"" << str1 << "\" is equal to \"" << str2 << "\"" << endl; StrCopy(str1, str2); cout << "\nAfter copying:\n1st string is \"" << str1 << "\" and\n2nd string is \"" << StrCopy(str2, "like") << "\"" << endl; StrConcat(str1, str2); cout << "\nAfter concatenating:\n1st string is \"" << str1 << "\" and\n2nd string is \"" << StrConcat(str2, "ness") << "\"" << endl; cout << endl; return 0; } /*********************** Function Definitions **********************/ int StrLength(char *str) { //YOU FILL IN! } int StrCompare(char *str1, char *str2) { //YOU FILL IN! } char *StrCopy(char *dest, char *source) { //YOU FILL IN! } char *StrConcat(char *dest, char *source) { //YOU FILL IN! }