// str1.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program shows the use of some of the build-in string functions: // strcpy ... strncpy ... strcat ... strncat ... strcmp ... strncmp // strchr ... strrchr ... strlen & strstr #include "str1.h" StringManipulation::StringManipulation( void ) { str1 = new char [ MAXSTRINGLEN ]; str2 = new char [ MAXSTRINGLEN ]; str3 = new char [ MAXSTRINGLEN ]; str4 = new char [ DOUBLEMAXSTRINGLEN ]; str5 = new char [ DOUBLEMAXSTRINGLEN ]; } void StringManipulation::StringOperations( void ) { int res = 0, len1 = 0, len2 = 0, count = 0; char ch; cout << " Please enter your first string: "; cin.getline( str1, MAXSTRINGLEN ); cout << " Please enter your second string: "; cin.getline( str2, MAXSTRINGLEN ); cout << " \n Now will start performing some string operations on these strings: " << endl; cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " << endl; cout << " Comparing 1st string to 2nd string: " << endl; cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " << endl; if ( ( res = strcmp( str1, str2 ) ) == 0 ) cout << " 1st and 2nd strings are identical. " << endl; else if ( res > 0 ) cout << " 1st string is *alphabetically* bigger than the 2nd string. " << endl; else cout << " 2nd string is *alphabetically* bigger than the 1st string. " << endl; // if both strings are not the same, do the following operations: if ( res != 0 ) { cout << " \n Will test which string is longer.. " << endl; cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " << endl; if ( ( len1 = strlen( str1 ) ) == ( len2 = strlen( str2 ) ) ) cout << " Both strings have the same number of characters. " << endl; else if ( len1 < len2 ) cout << " 1st string has less characters than 2nd string. " << endl; else cout << " 1st string has more characters than 2nd string. " << endl; } // If the strings are not of the same length, try to compare parts of them. if ( len1 != len2 ) { cout << " \n The strings are neither identical, nor of the same length; " << endl << " So you will be allowed to compare parts of them from \ their beginnings. " << endl << " Please enter the number of characters you wish to compare; " << endl << " this number should be at most: " << min( len1, len2 ) << endl; cin >> count; if ( ( res = strncmp( str1, str2, count ) ) == 0 ) cout << " \n The first " << count << " characters of the strings are identical. " << endl; else if ( res < 0 ) cout << " \n The first " << count << " characters of the 1st string is \ alphabetically smaller than the 2nd one. " << endl; else cout << "\n The first " << count << " characters of the 1st string is \ alphabetically bigger than the 2nd one. " << endl; } // Anyways you can find whether some specific character is included // in any string or not. cout << " \n Now will test looking for a specific character in a string. " << endl; cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " << endl; cout << "\n Enter the character that you want to look for: "; cin >> ch; if ( strchr( str1, ch ) != NULL ) { strcpy( str3, strchr( str1, ch ) ); cout << " \n The character ' " << ch << " ' was found in the 1st string. " << endl; cout << " Here is its *first* appearance: " << str3 << endl; } else cout << " \n The character ' " << ch << " ' was not found in the 1st string. " << endl; if ( strrchr( str2, ch ) != NULL ) { strcpy( str3, strrchr( str2, ch )); cout << " \n The character ' " << ch << " ' was found in the 2nd string. " << endl; cout << " Here is its *last* appearance: " << str3 << endl; } else cout << " \n The character ' " << ch << " ' was not found in the 2nd string. " << endl; // If there is no last occurrence, there is no first one for sure! cout << " \n Now will test the concatination processs: " << endl; cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " << endl; // Concatenate the two strings strcpy( str4, str1 ); strcat( str4, str2 ); cout << "\n The concatenation of the 2nd string to the 1st string \ results on: " << str4 << endl; strcpy( str4, str2); strncat( str4, str1, count ); cout << " \n The concatenation of " << count << " characters of the 1st string to the 2nd string results on: " << str4 << endl; strcpy( str5, str1); strncpy( str5, str2, count ); cout << " Notice carefully how strncpy works. Here is the result of \ the above two operations: " << str5 << endl; } int StringManipulation::min( int x, int y ) { return x < y ? x : y; } StringManipulation::~StringManipulation( void ) { delete []str1; delete []str2; delete []str3; delete []str4; delete []str5; } void main( void ) { StringManipulation strmanipulator; strmanipulator.StringOperations( ); } // The result of running the program: /* Please enter your first string: Getting started with strings Please enter your second string: That could be very interesting! Now will start performing some string operations on these strings: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Comparing 1st string to 2nd string: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2nd string is *alphabetically* bigger than the 1st string. Will test which string is longer.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1st string has less characters than 2nd string. The strings are neither identical, nor of the same length; So you will be allowed to compare parts of them from their beginnings. Please enter the number of characters you wish to compare; this number should be at most: 28 6 The first 6 characters of the 1st string is alphabetically smaller than the 2nd one. Now will test looking for a specific character in a string. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter the character that you want to look for: s The character ' s ' was found in the 1st string. Here is its *first* appearance: started with strings The character ' s ' was found in the 2nd string. Here is its *last* appearance: sting! Now will test the concatination processs: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The concatenation of the 2nd string to the 1st string results on: Getting started with stringsThat could be very interesting! The concatenation of 6 characters of the 1st string to the 2nd string results on: That could be very interesting!Gettin Notice carefully how strncpy works. Here is the result of the above two operations: That cg started with strings */ // ******** Run again ******** /* Please enter your first string: Hello Please enter your second string: Hello again Now will start performing some string operations on these strings: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Comparing 1st string to 2nd string: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2nd string is *alphabetically* bigger than the 1st string. Will test which string is longer.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1st string has less characters than 2nd string. The strings are neither identical, nor of the same length; So you will be allowed to compare parts of them from their beginnings. Please enter the number of characters you wish to compare; this number should be at most: 5 4 The first 4 characters of the strings are identical. Now will test looking for a specific character in a string. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter the character that you want to look for: l The character ' l ' was found in the 1st string. Here is its *first* appearance: llo The character ' l ' was found in the 2nd string. Here is its *last* appearance: lo again Now will test the concatination processs: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The concatenation of the 2nd string to the 1st string results on: HelloHello again The concatenation of 4 characters of the 1st string to the 2nd string results on: Hello againHell Notice carefully how strncpy works. Here is the result of the above two operations: Hello */