# include # include # include using namespace std; # include "chrpak.H" int main ( void ); void test001 ( void ); void test005 ( void ); void test0055 ( void ); void test011 ( void ); void test012 ( void ); void test014 ( void ); void test015 ( void ); void test016 ( void ); void test0175 ( void ); void test026 ( void ); void test029 ( void ); void test0545 ( void ); void test058 ( void ); void test065 ( void ); void test085 ( void ); void test090 ( void ); void test091 ( void ); void test093 ( void ); void test1015 ( void ); void test102 ( void ); void test1035 ( void ); void test1036 ( void ); void test105 ( void ); void test109 ( void ); void test1125 ( void ); void test1126 ( void ); void test115 ( void ); void test119 ( void ); void test1225 ( void ); void test1255 ( void ); void test1265 ( void ); void test129 ( void ); void test137 ( void ); void test138 ( void ); void test154 ( void ); //****************************************************************************80 int main ( void ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for CHRPAK_PRB. // // Discussion: // // CHRPAK_PRB calls the CHRPAK tests. // // Modified: // // 15 June 2007 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "CHRPAK_PRB:\n"; cout << " C++ version\n"; cout << "\n"; cout << " Test the routines in the CHRPAK library.\n"; print_sizes ( ); test001 ( ); // // TEST005 not ready, since BASE_TO_I4 not available yet. // //test005 ( ); test0055 ( ); test011 ( ); test012 ( ); test014 ( ); test015 ( ); test016 ( ); test0175 ( ); test026 ( ); test029 ( ); test0545 ( ); test058 ( ); test065 ( ); test085 ( ); test090 ( ); test091 ( ); test093 ( ); test1015 ( ); test102 ( ); test1035 ( ); test1036 ( ); test105 ( ); test109 ( ); test1125 ( ); test1126 ( ); test115 ( ); test119 ( ); test1225 ( ); test1255 ( ); test1265 ( ); test129 ( ); test137 ( ); test138 ( ); test154 ( ); cout << "\n"; cout << "CHRPAK_PRB:\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void test001 ( void ) //****************************************************************************80 // // Purpose: // // TEST001 tests A_TO_I4 and I4_TO_A. // // Modified: // // 09 June 2007 // // Author: // // John Burkardt // { char a; int i; int i2; cout << "\n"; cout << "TEST001\n"; cout << " A_TO_I4: Alphabetic character => I\n"; cout << " I4_TO_A: I => Alphabetic character\n"; cout << "\n"; cout << " 1:26 = A:Z\n"; cout << " 27:52 = a:z\n"; cout << "\n"; cout << " I ==> A ==> I\n"; cout << "\n"; for ( i = 0; i <= 55; i = i + 3 ) { a = i4_to_a ( i ); i2 = a_to_i4 ( a ); cout << " " << setw(8) << i << " '" << a << "'" << " " << setw(8) << i2 << "\n"; } return; } //****************************************************************************80 void test005 ( void ) //****************************************************************************80 // // Purpose: // // TEST005 tests BASE_TO_I4 and I4_TO_BASE. // // Modified: // // 14 January 2007 // // Author: // // John Burkardt // { # define TEST_NUM 6 int base; int base_test[TEST_NUM] = { -1, 1, 2, 3, 4, 8 }; int i1; int i2; int i_test[TEST_NUM] = { 5, 5, 21, -243, 16, 15 }; char *s; int test; cout << "\n"; cout << "TEST005\n"; cout << " BASE_TO_I4 converts an integer in some other\n"; cout << " base into base 10.\n"; cout << " I4_TO_BASE converts an integer base 10 to \n"; cout << " its representation in another base;\n"; cout << "\n"; cout << " BASE, I, I4_TO_BASE(I), BASE_TO_I4(I4_TO_BASE(I))\n"; cout << "\n"; for ( test = 0; test < TEST_NUM; test++ ) { i1 = i_test[test]; base = base_test[test]; // s = i4_to_base ( i1, base ); i2 = base_to_i4 ( s, base ); cout << " " << setw(8) << base << " " << setw(8) << i1 << " " << setw(20) << s << " " << setw(8) << i2 << "\n"; delete [] s; } return; # undef TEST_NUM } //****************************************************************************80 void test0055 ( void ) //****************************************************************************80 // // Purpose: // // TEST0055 tests BYTE_TO_INT and INT_TO_BYTE. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { int ival; unsigned int ival2; unsigned char string[4]; cout << "\n"; cout << "TEST0055\n"; cout << " INT_TO_BYTE converts an unsigned int to a string,\n"; cout << " BYTE_TO_INT converts it back.\n"; cout << "\n"; cout << " IVAL Recovered IVAL\n"; cout << "\n"; ival = 3; int_to_byte ( ival, string ); byte_to_int ( string, &ival2 ); cout << " " << setw(9) << ival << " " << setw(9) << ival2 << "\n"; ival = 1952; int_to_byte ( ival, string ); byte_to_int ( string, &ival2 ); cout << " " << setw(9) << ival << " " << setw(9) << ival2 << "\n"; ival = 123456789; int_to_byte ( ival, string ); byte_to_int ( string, &ival2 ); cout << " " << setw(9) << ival << " " << setw(9) << ival2 << "\n"; return; } //****************************************************************************80 void test011 ( void ) //****************************************************************************80 // // Purpose: // // TEST011 tests CH_CAP. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char c; cout << "\n"; cout << "TEST011\n"; cout << " CH_CAP uppercases a character.\n"; cout << "\n"; cout << " C CH_CAP(C)\n"; cout << "\n"; c = 'F'; cout << " " << c << " " << ch_cap ( c ) << "\n"; c = 'f'; cout << " " << c << " " << ch_cap ( c ) << "\n"; c = '1'; cout << " " << c << " " << ch_cap ( c ) << "\n"; c = 'b'; cout << " " << c << " " << ch_cap ( c ) << "\n"; c = 'B'; cout << " " << c << " " << ch_cap ( c ) << "\n"; return; } //****************************************************************************80 void test012 ( void ) //****************************************************************************80 // // Purpose: // // TEST012 tests CH_COUNT_FILE_ADD. // // Modified: // // 22 September 2005 // // Author: // // John Burkardt // { int count[256]; cout << "\n"; cout << "TEST012\n"; cout << " CH_COUNT_FILE_ADD adds the characters in a file\n"; cout << " to a character count.\n"; cout << " DEBUG, call C_COUNT_INIT:\n"; ch_count_init ( count ); cout << " DEBUG, call C_COUNT_FILE_ADD:\n"; ch_count_file_add ( "chrpak_prb.C", count ); cout << " DEBUG, call C_COUNT_PRINT:\n"; ch_count_print ( count, "Raw character count data:" ); return; } //****************************************************************************80 void test014 ( void ) //****************************************************************************80 // // Purpose: // // TEST014 tests CH_INDEX. // // Modified: // // 10 June 2007 // // Author: // // John Burkardt // { char c; int j; char s[40]; cout << "\n"; cout << "TEST014\n"; cout << " CH_INDEX searches a string for a character.\n"; strcpy ( s, "Joel prefers graphics to graphs." ); cout << "\n"; cout << " The test string, in quotes:\n"; cout << "\n"; cout << " \"" << s << "\"\n"; c = 'g'; j = ch_index ( s, c ); cout << " The first occurrence of '" << c << "' is at index " << j << ".\n"; return; } //****************************************************************************80 void test015 ( void ) //****************************************************************************80 // // Purpose: // // TEST015 tests CH_INDEX_LAST. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char c; int j; char s[25]; cout << "\n"; cout << "TEST015\n"; cout << " CH_INDEX_LAST finds the last occurrence of a character.\n"; strcpy ( s, "HELLO World, how ARE you?" ); cout << "\n"; cout << " The test string, in quotes:\n"; cout << "\n"; cout << " \"" << s << "\"\n"; c = 'o'; j = ch_index_last ( s, c ); cout << " Last occurrence of " << c << " is at " << j << ".\n"; return; } //****************************************************************************80 void test016 ( void ) //****************************************************************************80 // // Purpose: // // TEST016 tests CH_LOW. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { # define TEST_NUM 5 char c1; char c2; char c_test[TEST_NUM] = { 'F', 'f', '1', 'b', 'B' }; int test; cout << "\n"; cout << "TEST016\n"; cout << " CH_LOW lowercases a character.\n"; cout << "\n"; cout << " C CH_LOW(C)\n"; cout << "\n"; for ( test = 0; test < TEST_NUM; test++ ) { c1 = c_test[test]; c2 = ch_low ( c1 ); cout << " " << c1 << " " << c2 << "\n"; } return; # undef TEST_NUM } //****************************************************************************80 void test0175 ( void ) //****************************************************************************80 // // Purpose: // // TEST0175 tests CH_PAD. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { int ch_index; int null_index; int result; char s[25]; cout << "\n"; cout << "TEST0175\n"; cout << " CH_PAD places spaces around a character.\n"; cout << "\n"; strcpy ( s, "I vant to be alone!" ); cout << " The string is : \"" << s << "\"\n"; cout << " We will try to place spaces around the A in ALONE.\n"; ch_index = 13; null_index = strlen ( s ); result = ch_pad ( &ch_index, &null_index, s, 25 ); if ( result != 0 ) { cout << " The operation failed.\n"; } else { cout << " The string is : \"" << s << "\"\n"; } return; } //****************************************************************************80 void test026 ( void ) //****************************************************************************80 // // Purpose: // // TEST026 tests CH_TO_ROT13. // // Modified: // // 23 March 2006 // // Author: // // John Burkardt // { int i; char s1[80]; int s1_length; char s2[80]; char s3[80]; cout << "\n"; cout << "TEST026\n"; cout << " CH_TO_ROT13 \"encodes\" a character using ROT13.\n"; strcpy ( s1, "ABCDEFGHIJKLMNOPQRSTUVQXYZ" ); s1_length = s_len_trim ( s1 ); strcpy ( s2, " " ); strcpy ( s3, " " ); // // Notice that we also copy the trailing NULL character from S1 to S2 and S3. // for ( i = 0; i <= s1_length; i++ ) { *(s2+i) = ch_to_rot13 ( *(s1+i) ); *(s3+i) = ch_to_rot13 ( *(s2+i) ); } cout << "\n"; cout << " CH :" << s1 << "\n"; cout << " ROT13(CH) :" << s2 << "\n"; cout << " ROT13(ROT13(CH)):" << s3 << "\n"; strcpy ( s1, " CH_TO_ROT13 \"encodes\" a character using ROT13." ); s1_length = s_len_trim ( s1 ); strcpy ( s2, " " ); strcpy ( s3, " " ); for ( i = 0; i <= s1_length; i++ ) { *(s2+i) = ch_to_rot13 ( *(s1+i) ); *(s3+i) = ch_to_rot13 ( *(s2+i) ); } cout << "\n"; cout << " CH :" << s1 << "\n"; cout << " ROT13(CH) :" << s2 << "\n"; cout << " ROT13(ROT13(CH)):" << s3 << "\n"; return; } //****************************************************************************80 void test029 ( void ) //****************************************************************************80 // // Purpose: // // TEST029 tests CH_UNIFORM. // // Modified: // // 10 June 2007 // // Author: // // John Burkardt // { char ch; char chi; char clo; int count[26]; int i; int j; int seed; cout << "\n"; cout << "TEST029\n"; cout << " CH_UNIFORM returns a random character.\n"; for ( i = 0; i < 26; i++ ) { count[i] = 0; } clo = 'D'; chi = 'W'; seed = 123456789; cout << "\n"; cout << " I A Count\n"; cout << "\n"; for ( i = 1; i <= 100000; i++ ) { ch = ch_uniform ( clo, chi, &seed ); j = a_to_i4 ( ch ); count[j-1] = count[j-1] + 1; } for ( i = 1; i <= 26; i++ ) { cout << " " << setw(2) << i << " " << i4_to_a ( i ) << " " << setw(5) << count[i-1] << "\n"; } return; } //****************************************************************************80 void test0545 ( void ) //****************************************************************************80 // // Purpose: // // TEST0545 tests I4_TO_MONTH_ABB. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { int i; char *s; cout << "\n"; cout << "TEST0545\n"; cout << " I4_TO_MONTH_ABB returns the name of the I-th month.\n"; cout << "\n"; cout << " I Month\n"; cout << "\n"; for ( i = 0; i <= 12; i++ ) { s = i4_to_month_abb ( i ) ; cout << " " << i << " " << s << "\n"; } return; } //****************************************************************************80 void test058 ( void ) //****************************************************************************80 // // Purpose: // // TEST058 tests I4_TO_S and S_TO_I4. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { bool error; int i; int last; char s1[20]; char *s2; cout << "\n"; cout << "TEST058\n"; cout << " I4_TO_S: int -> string;\n"; cout << " S_TO_I4: string -> I4.\n"; cout << "\n"; strcpy ( s1, "0" ); i = s_to_i4 ( s1, &last, &error ); s2 = i4_to_s ( i ); cout << " \"" << setw(20) << s1 << "\" " << setw(6) << i << " " << "\"" << setw(20) << s2 << "\"\n"; strcpy ( s1, "9" ); i = s_to_i4 ( s1, &last, &error ); s2 = i4_to_s ( i ); cout << " \"" << setw(20) << s1 << "\" " << setw(6) << i << " " << "\"" << setw(20) << s2 << "\"\n"; strcpy ( s1, "10" ); i = s_to_i4 ( s1, &last, &error ); s2 = i4_to_s ( i ); cout << " \"" << setw(20) << s1 << "\" " << setw(6) << i << " " << "\"" << setw(20) << s2 << "\"\n"; strcpy ( s1, "11" ); i = s_to_i4 ( s1, &last, &error ); s2 = i4_to_s ( i ); cout << " \"" << setw(20) << s1 << "\" " << setw(6) << i << " " << "\"" << setw(20) << s2 << "\"\n"; strcpy ( s1, " -124 56 AbC" ); i = s_to_i4 ( s1, &last, &error ); s2 = i4_to_s ( i ); cout << " \"" << setw(20) << s1 << "\" " << setw(6) << i << " " << "\"" << setw(20) << s2 << "\"\n"; strcpy ( s1, "25,50,5" ); i = s_to_i4 ( s1, &last, &error ); s2 = i4_to_s ( i ); cout << " \"" << setw(20) << s1 << "\" " << setw(6) << i << " " << "\"" << setw(20) << s2 << "\"\n"; strcpy ( s1, "+15.9" ); i = s_to_i4 ( s1, &last, &error ); s2 = i4_to_s ( i ); cout << " \"" << setw(20) << s1 << "\" " << setw(6) << i << " " << "\"" << setw(20) << s2 << "\"\n"; strcpy ( s1, " 123abc" ); i = s_to_i4 ( s1, &last, &error ); s2 = i4_to_s ( i ); cout << " \"" << setw(20) << s1 << "\" " << setw(6) << i << " " << "\"" << setw(20) << s2 << "\"\n"; return; } //****************************************************************************80 void test065 ( void ) //****************************************************************************80 // // Purpose: // // TEST065 tests I4_TO_UNARY. // // Modified: // // 09 June 2007 // // Author: // // John Burkardt // { # define TEST_NUM 3 int i4; int i4_test[TEST_NUM] = { -5, 0, 7 }; char *s; int test; cout << "\n"; cout << "TEST065\n"; cout << " I4_TO_UNARY converts an integer to unary.\n"; cout << "\n"; cout << " I4 I4_TO_UNARY(I4)\n"; cout << "\n"; for ( test = 0; test < TEST_NUM; test++ ) { i4 = i4_test[test]; s = i4_to_unary ( i4 ); cout << " " << setw(8) << i4 << " '" << s << "'\n"; delete [] s; } return; # undef TEST_NUM } //****************************************************************************80 void test085 ( void ) //****************************************************************************80 // // Purpose: // // TEST085 tests S_ADJUSTL. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char s[15]; char s2[15]; cout << "\n"; cout << "TEST085\n"; cout << " S_ADJUSTL justifies a string to the left;\n"; cout << "\n"; cout << " Original S_ADJUSTL\n"; cout << " ---------- ---------- \n"; cout << "\n"; strcpy ( s, " Hello! " ); strcpy ( s2, s ); s_adjustl ( s2 ); cout << setw(10) << "\"" << s << "\" " << setw(10) << "\"" << s2 << "\"\n"; strcpy ( s, "Ouch!" ); strcpy ( s2, s ); s_adjustl ( s2 ); cout << setw(10) << "\"" << s << "\" " << setw(10) << "\"" << s2 << "\"\n"; strcpy ( s, " A B C " ); strcpy ( s2, s ); s_adjustl ( s2 ); cout << setw(10) << "\"" << s << "\" " << setw(10) << "\"" << s2 << "\"\n"; return; } //****************************************************************************80 void test090 ( void ) //****************************************************************************80 // // Purpose: // // TEST090 tests S_BEGIN. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { int i; char s1[80]; char s2[80]; cout << "\n"; cout << "TEST090\n"; cout << " S_BEGIN is true if string 1 begins with string 2.\n"; strcpy ( s1, "Look for the lily in the field." ); strcpy ( s2, "Look for" ); cout << "\n"; cout << " String 1:\n"; cout << "\n"; cout << " \"" << s1 << "\"\n"; cout << "\n"; cout << " String2:\n"; cout << "\n"; cout << " \"" << s2 << "\"\n"; cout << "\n"; cout << " SBEGIN ( S1, S2 ) = " << s_begin ( s1, s2 ) << "\n"; strcpy ( s1, "Look for" ); strcpy ( s2, "Look for the lily in the field." ); cout << "\n"; cout << " String 1:\n"; cout << "\n"; cout << " \"" << s1 << "\"\n"; cout << "\n"; cout << " String2:\n"; cout << "\n"; cout << " \"" << s2 << "\"\n"; cout << "\n"; cout << " SBEGIN ( S1, S2 ) = " << s_begin ( s1, s2 ) << "\n"; strcpy ( s1, "Look for the lily in the field." ); strcpy ( s2, "Look out!" ); cout << "\n"; cout << " String 1:\n"; cout << "\n"; cout << " \"" << s1 << "\"\n"; cout << "\n"; cout << " String2:\n"; cout << "\n"; cout << " \"" << s2 << "\"\n"; cout << "\n"; cout << " SBEGIN ( S1, S2 ) = " << s_begin ( s1, s2 ) << "\n"; return; } //****************************************************************************80 void test091 ( void ) //****************************************************************************80 // // Purpose: // // TEST091 tests S_BEHEAD_SUBSTRING. // // Modified: // // 30 January 2006 // // Author: // // John Burkardt // { # define TEST_NUM 4 int i; char s[21]; char s_old[21]; char sub[21]; cout << "\n"; cout << "TEST091\n"; cout << " S_BEHEAD_SUBSTRING removes an initial substring from a \n"; cout << " string, if it occurs.\n"; cout << "\n"; cout << " ------String-------- -----SUB------------ ---Beheaded----\n"; cout << "\n"; for ( i = 1; i <= TEST_NUM; i++ ) { if ( i == 1 ) { strcpy ( s, " HELLO World!" ); strcpy ( sub, "HELLO" ); } else if ( i == 2 ) { strcpy ( s, "12345678901234567890" ); strcpy ( sub, "12345" ); } else if ( i == 3 ) { strcpy ( s, "0.314159E+01" ); strcpy ( sub, "314" ); } else if ( i == 4 ) { strcpy ( s, "!@#$%a^&A(){}[]\\|<>?" ); strcpy ( sub, "!@#$%a^&A(){}[]\\|<>?" ); } strcpy ( s_old, s ); s_behead_substring ( s, sub ); cout << " " << setw(20) << s_old << " " << setw(20) << sub << " " << setw(20) << s << "\n"; } return; # undef TEST_NUM } //****************************************************************************80 void test093 ( void ) //****************************************************************************80 // // Purpose: // // TEST093 tests S_BLANKS_DELETE. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char s[25]; strcpy ( s, " HELLO World !! ! " ); cout << "\n"; cout << "TEST093\n"; cout << " S_BLANKS_DELETE removes double blanks.\n"; cout << "\n"; cout << " Original string:\n"; cout << "\n"; cout << " \"" << s << "\"\n"; s_blanks_delete ( s ); cout << "\n"; cout << " After S_BLANKS_DELETE:\n"; cout << "\n"; cout << " \"" << s << "\"\n"; return; } //****************************************************************************80 void test1015 ( void ) //****************************************************************************80 // // Purpose: // // TEST1015 tests S_EQI. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char s1[10]; char s2[8]; cout << "\n"; cout << "TEST1015\n"; cout << " S_EQI reports if two strings are equal, ignoring case.\n"; strcpy ( s1, "HELLO" ); strcpy ( s2, "HeLLO" ); cout << "\n"; cout << " String 1:\n"; cout << "\n"; cout << " \"" << s1 << "\"\n"; cout << "\n"; cout << " String2:\n"; cout << "\n"; cout << " \"" << s2 << "\"\n"; cout << "\n"; cout << " S_EQI(S1,S2) = " << s_eqi ( s1, s2 ) << "\n"; strcpy ( s1, "HELP ME" ); strcpy ( s2, "HELP" ); cout << "\n"; cout << " String 1:\n"; cout << "\n"; cout << " \"" << s1 << "\"\n"; cout << "\n"; cout << " String2:\n"; cout << "\n"; cout << " \"" << s2 << "\"\n"; cout << "\n"; cout << " S_EQI(S1,S2) = " << s_eqi ( s1, s2 ) << "\n"; return; } //****************************************************************************80 void test102 ( void ) //****************************************************************************80 // // Purpose: // // TEST102 tests S_ESCAPE_TEX. // // Discussion: // // Since "\" is also a C++ escape character, to do this example we have // to enter "\\" in the initialization of S1; this actually puts just // a plain old "\" there. Oh, the joys of escape sequences! // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char s1[] = "The file A_B.TXT is {I think__so} of size 2^8 or C\\B."; char *s2; cout << "\n"; cout << "TEST102\n"; cout << " S_ESCAPE_TEX \"protects\" characters in a string\n"; cout << " that might otherwise be interpreted as TeX\n"; cout << " escape characters.\n"; cout << "\n"; cout << " Original string:\n"; cout << "\n"; cout << " \"" << s1 << "\".\n"; s2 = s_escape_tex ( s1 ); cout << "\n"; cout << " De-escaped string:\n"; cout << "\n"; cout << " \"" << s2 << "\".\n"; return; } //****************************************************************************80 void test1035 ( void ) //****************************************************************************80 // // Purpose: // // TEST1035 tests S_FIRST_CH. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char c; char s[80]; char* t; cout << "\n"; cout << "TEST1035\n"; cout << " S_FIRST_CH finds the first occurrence of a character.\n"; strcpy ( s, "Look for the lily in the field." ); cout << "\n"; cout << " The test string, in quotes:\n"; cout << "\n"; cout << " \"" << s << "\"\n"; c = 'l'; t = s_first_ch ( s, c ); cout << "\n"; cout << " The string, starting with the first occurrence of '" << c << "':\n"; cout << "\n"; cout << " \"" << t << "\"\n"; return; } //****************************************************************************80 void test1036 ( void ) //****************************************************************************80 // // Purpose: // // TEST1036 tests S_FIRST_NONBLANK. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char *first; char s[40]; cout << "\n"; cout << "TEST1036\n"; cout << " S_FIRST_NONBLANK finds a pointer to the first \n"; cout << " nonblank character in a string.\n"; strcpy ( s, " HELLO World, how ARE you?" ); cout << "\n"; cout << " The test string, in quotes:\n"; cout << "\n"; cout << " \"" << s << "\"\n"; first = s_first_nonblank ( s ); cout << "\n"; cout << " The string shifted left, using the pointer:\n"; cout << "\n"; cout << " \"" << first << "\"\n"; return; } //****************************************************************************80 void test105 ( void ) //****************************************************************************80 // // Purpose: // // TEST105 tests S_INC_C. // // Modified: // // 11 June 2007 // // Author: // // John Burkardt // { int i; int ierror; char s[30]; cout << "\n"; cout << "TEST105\n"; cout << " S_INC_C can \"increment\" the characters in a string.\n"; strcpy ( s, "Tax" ); cout << "\n"; cout << " Starting string: \"" << s << "\"\n"; cout << "\n"; cout << " Incremented forms:\n"; cout << "\n"; for ( i = 1; i <= 5; i++ ) { s_inc_c ( s ); cout << " " << s << "\n"; } strcpy ( s, "aB34c* 8zY" ); cout << "\n"; cout << " Starting string: \"" << s << "\"\n"; cout << "\n"; cout << " Incremented forms:\n"; cout << "\n"; for ( i = 1; i <= 5; i++ ) { s_inc_c ( s ); cout << " " << s << "\n"; } return; } //****************************************************************************80 void test109 ( void ) //****************************************************************************80 // // Purpose: // // TEST109 tests S_INDEX_LAST_C. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char c; int i; int j; char *s = "The quick brown fox jumps right over the big lazy dog!"; cout << "\n"; cout << "TEST109\n"; cout << " S_INDEX_LAST_C reports the LAST occurrence\n"; cout << " of a character.\n"; cout << "\n"; cout << " String = " << s << "\n"; cout << "\n"; cout << " I C J\n"; cout << "\n"; for ( i = 27; i <= 52; i++ ) { c = i4_to_a ( i ); j = s_index_last_c ( s, c ); cout << setw(6) << i << " " << setw(1) << c << " " << setw(6) << j << "\n"; } return; } //****************************************************************************80 void test1125 ( void ) //****************************************************************************80 // // Purpose: // // TEST1125 tests S_LAST_CH. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char c; char s[80]; char* t; cout << "\n"; cout << "TEST1125\n"; cout << " S_LAST_CH finds the last occurrence of a character.\n"; strcpy ( s, "Look for last . in file_name.cpp" ); cout << "\n"; cout << " The test string, in quotes:\n"; cout << "\n"; cout << " \"" << s << "\"\n"; c = '.'; t = s_last_ch ( s, c ); cout << "\n"; cout << " The string, starting with the last occurrence of '" << c << "':\n"; cout << "\n"; cout << " \"" << t << "\"\n"; return; } //****************************************************************************80 void test1126 ( void ) //****************************************************************************80 // // Purpose: // // TEST1126 tests S_LEN_TRIM. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { int i; char s[11]; int test; cout << "\n"; cout << "TEST1126\n"; cout << " S_LEN_TRIM reports the length of a string to the last nonblank.\n"; cout << "\n"; cout << " Here are some strings, and their lengths:\n"; cout << "\n"; for ( test = 0; test < 4; test++ ) { if ( test == 0 ) { strcpy ( s, "HELLO" ); } else if ( test == 1 ) { strcpy ( s, " B la nk" ); } else if ( test == 2 ) { strcpy ( s, " "); } else if ( test == 3 ) { strcpy ( s, "1234567890" ); } cout << " \"" << s << "\" " << s_len_trim ( s ) << "\n"; } return; } //****************************************************************************80 void test115 ( void ) //****************************************************************************80 // // Purpose: // // TEST115 tests S_REPLACE_CH. // // Modified: // // 30 January 2006 // // Author: // // John Burkardt // { char c1; char c2; char s[16]; char *s_old = "No pennies now."; cout << "\n"; cout << "TEST115\n"; cout << " S_REPLACE_CH replaces one character by another;\n"; cout << "\n"; cout << " C1 C2 Original String Modified String\n"; cout << "\n"; c1 = 'n'; c2 = 't'; strcpy ( s, s_old ); s_replace_ch ( s, c1, c2 ); cout << " " << c1 << " " << c2 << " " << s_old << " " << s << "\n"; return; } //****************************************************************************80 void test119 ( void ) //****************************************************************************80 // // Purpose: // // TEST119 tests S_REVERSE. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char s[36] = "A man, a plan, a canal, Panama!"; cout << "\n"; cout << "TEST119\n"; cout << " S_REVERSE reverses a string.\n"; cout << "\n"; cout << " Before: \"" << s << "\".\n"; s_reverse ( s ); cout << " After: \"" << s << "\".\n"; return; } //****************************************************************************80 void test1265 ( void ) //****************************************************************************80 // // Purpose: // // TEST1265 tests S_SUBSTRING. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { int a; int b; char s[21]; int test; cout << "\n"; cout << "TEST1265\n"; cout << " S_SUBSTRING returns a substring of a given string.\n"; cout << "\n"; cout << " S A B S(A:B)\n"; cout << " -------------------- -- -- ----------\n"; cout << "\n"; strcpy ( s, "abcdefghijklmnopqrts" ); a = 6; b = 10; cout << " " << setw(20) << s << " " << setw(2) << a << " " << setw(2) << b << " " << s_substring ( s, a, b ) << "\n"; a = 15; b = 15; cout << " " << setw(20) << s << " " << setw(2) << a << " " << setw(2) << b << " " << s_substring ( s, a, b ) << "\n"; a = 17; b = 20; cout << " " << setw(20) << s << " " << setw(2) << a << " " << setw(2) << b << " " << s_substring ( s, a, b ) << "\n"; return; } //****************************************************************************80 void test1225 ( void ) //****************************************************************************80 // // Purpose: // // TEST1225 tests S_S_SUBANAGRAM. // // Modified: // // 15 June 2007 // // Author: // // John Burkardt // { # define TEST_NUM 4 char s1[14]; char *s1_test[TEST_NUM] = { "Get a priest!", "Get a priest!", "Get a priest!", "Get a priest!" }; char s2[7]; char *s2_test[TEST_NUM] = { "stripe", "pastor", "a sip", "tag!" }; int test; bool value; cout << "\n"; cout << "TEST1225\n"; cout << " S_S_SUBANAGRAM is TRUE if S2 is a \"subanagram\"\n"; cout << " of S1.\n"; cout << "\n"; for ( test = 0; test < TEST_NUM; test++ ) { strcpy ( s1, s1_test[test] ); strcpy ( s2, s2_test[test] ); value = s_s_subanagram ( s1, s2 ); cout << " \"" << s1_test[test] << "\"" << " \"" << s2_test[test] << "\"" << " " << value << "\n"; } return; # undef TEST_NUM } //****************************************************************************80 void test1255 ( void ) //****************************************************************************80 // // Purpose: // // TEST1255 tests S_SORT_A. // // Modified: // // 15 June 2007 // // Author: // // John Burkardt // { # define TEST_NUM 5 char s[21]; char *s_test[TEST_NUM] = { "HELLO World !! ! ", "12345678901234567890", "Abc Def Ghi Jkl Mno ", "AbleBakerCharlieDelt", "What? You have seen?" }; int test; cout << "\n"; cout << "TEST1255\n"; cout << " S_SORT_A ascending sorts a string.\n"; cout << "\n"; cout << " -------String------- -------Sorted-------\n"; cout << "\n"; for ( test = 0; test < TEST_NUM; test++ ) { strcpy ( s, s_test[test] ); s_sort_a ( s ); cout << " " << "\"" << s_test[test] << "\"" << " " << "\"" << s << "\"" << "\n"; } return; # undef TEST_NUM } //****************************************************************************80 void test129 ( void ) //****************************************************************************80 // // Purpose: // // TEST129 tests S_TO_FORMAT // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { # define TEST_NUM 7 char c; int i; int m; int r; char s[21]; int w; cout << "\n"; cout << "TEST129\n"; cout << " S_TO_FORMAT, string -> FORTRAN format RcW.M;\n"; cout << "\n"; cout << " --------String------ R c W M\n"; cout << "\n"; for ( i = 1; i <= TEST_NUM; i++ ) { if ( i == 1 ) { strcpy ( s, "a80" ); } else if ( i == 2 ) { strcpy ( s, "f8.4" ); } else if ( i == 3 ) { strcpy ( s, "3g14.6" ); } else if ( i == 4 ) { strcpy ( s, "i12" ); } else if ( i == 5 ) { strcpy ( s, "12l1" ); } else if ( i == 6 ) { strcpy ( s, "(10o11)" ); } else if ( i == 7 ) { strcpy ( s, " ( 5 z 11.7 )" ); } s_to_format ( s, &r, &c, &w, &m ); cout << " " << setw(20) << s << " " << setw(6) << r << " " << c << setw(6) << w << " " << setw(6) << m << "\n"; } return; # undef TEST_NUM } //****************************************************************************80 void test137 ( void ) //****************************************************************************80 // // Purpose: // // TEST137 tests S_WORD_COUNT. // // Modified: // // 19 January 2007 // // Author: // // John Burkardt // { char s[40]; cout << "\n"; cout << "TEST137\n"; cout << " S_WORD_COUNT counts the words in a string\n"; cout << "\n"; cout << " STRING Words\n"; cout << "\n"; strcpy ( s, "?" ); cout << " " << setw(32) << s << " " << setw(12) << s_word_count ( s ) << "\n"; strcpy ( s, "A man, a plan, a canal - Panama!" ); cout << " " << setw(32) << s << " " << setw(12) << s_word_count ( s ) << "\n"; strcpy ( s, " justone!word,-@#$ " ); cout << " " << setw(32) << s << " " << setw(12) << s_word_count ( s ) << "\n"; strcpy ( s, "How about a day in the park?" ); cout << " " << setw(32) << s << " " << setw(12) << s_word_count ( s ) << "\n"; return; } //****************************************************************************80 void test138 ( void ) //****************************************************************************80 // // Purpose: // // TEST138 tests S_WORD_EXTRACT_FIRST. // // Modified: // // 31 January 2006 // // Author: // // John Burkardt // { char s[80]; char *word; strcpy ( s, "Just an incontrovertible sample of text!" ); cout << "\n"; cout << "TEST138\n"; cout << " S_WORD_EXTRACT_FIRST extracts the first word from a string.\n"; cout << "\n"; cout << " Our input string is:\n"; cout << " \"" << s << "\".\n"; cout << "\n"; for ( ; ; ) { word = s_word_extract_first ( s ); if ( !word ) { cout << "\n"; cout << " Reached the last word.\n"; break; } cout << " \"" << word << "\"\n"; delete [] word; } return; } //****************************************************************************80 void test154 ( void ) //****************************************************************************80 // // Purpose: // // TEST154 tests WORD_NEXT_READ. // // Modified: // // 22 September 2005 // // Author: // // John Burkardt // { bool done; char *s = " Here is a string, (you see) with x[1] = {gamma}!"; char *w; int word_num; cout << "\n"; cout << "TEST154\n"; cout << " WORD_NEXT_READ returns each word from a string.\n"; cout << " It pays attention to various parentheses and brackets.\n"; cout << "\n"; cout << " We use the following string:\n"; cout << " \"" << s << "\".\n"; cout << "\n"; cout << " Here are the individual words:\n"; cout << "\n"; done = true; word_num = 0; for ( ; ; ) { w = word_next_read ( s, &done ); if ( done ) { cout << "\n"; cout << " Number of words was " << word_num << "\n"; break; } word_num = word_num + 1; cout << " " << setw(6) << word_num << " \"" << w << "\".\n"; delete [] w; } return; }