home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / i18nv104.zip / SAMPLE / WSTR / WSTR.C < prev    next >
C/C++ Source or Header  |  1996-02-13  |  9KB  |  172 lines

  1. /*****************************************************************************/
  2. /***     Include files                                                     ***/
  3. /*****************************************************************************/
  4.  
  5. #include <stdio.h>                      /* Standard IO functions.            */
  6.  
  7. #include <wchar.h>                      /* I18N definitions.                 */
  8.  
  9. /*****************************************************************************/
  10. /***     Global defines                                                    ***/
  11. /*****************************************************************************/
  12.  
  13. #define SAME 0
  14.  
  15. /*****************************************************************************/
  16. /***     Global variables                                                  ***/
  17. /*****************************************************************************/
  18.  
  19. wchar_t w_str1[100], w_str2[100];       /* Two wide strings used in function.*/
  20.  
  21. /*****************************************************************************/
  22. /***     Copy and concatenate functions                                    ***/
  23. /*****************************************************************************/
  24.  
  25. void copy_concat_functions()
  26. {
  27.                                         /* Print a header.                   */
  28.                                         /* Copy a wide string into a variable*/
  29.                                         /* Print out the string.             */
  30.                                         /* Print out the length of the string*/
  31.                                         /* Concatenate a string on the end.  */
  32.                                         /* Print out the new string.         */
  33.                                         /* Copy the first 6 characters to a  */
  34.                                         /*  different wide string.           */
  35.                                         /* Print out that string.            */
  36.                                         /* Concatenate 13 characters from a  */
  37.                                         /*  literal onto the end of the 6    */
  38.                                         /*  character string.                */
  39.                                         /* Print out the results.            */
  40.   printf("\nCopy and concat functions:\n");
  41.   printf("--------------------------\n");
  42.   wcscpy(w_str1, L"This is a wchar_t string.");
  43.   printf("Original string: %S\n", w_str1);
  44.   printf("Length of the string is: %d\n", wcslen(w_str1));
  45.   wcscat(w_str1, L"  Add this on the end!");
  46.   printf("Add to end of the string: %S\n", w_str1);
  47.   wcsncpy(w_str2, w_str1, 6);
  48.   printf("Copy the first 6 characters of the string: %S\n", w_str2);
  49.   wcsncat(w_str2, L"is an added substring.", 13);
  50.   printf("Concat 13 characters on the end: %S\n", w_str2);
  51. }
  52.  
  53. /*****************************************************************************/
  54. /***     Comparison functions                                              ***/
  55. /*****************************************************************************/
  56.  
  57. void compare_functions(void)
  58. {
  59.                                         /* Print a header.                   */
  60.                                         /* Copy literals into 2 wide strings.*/
  61.                                         /* Compare the two strings to see if */
  62.                                         /*  they are the same.               */
  63.                                         /* Compare the first 10 characters to*/
  64.                                         /*  see if they are the same.        */
  65.   printf("\nCompare functions:\n");
  66.   printf("------------------\n");
  67.   wcscpy(w_str1, L"This is string 1.");
  68.   wcscpy(w_str2, L"This is string 2.");
  69.   printf("Are the two strings the same? %s\n",
  70.          (wcscmp(w_str1, w_str2) == SAME) ? "Yes" : "No");
  71.   printf("Are the first 10 characters of the two strings the same? %s\n",
  72.          (wcsncmp(w_str1, w_str2, 10) == SAME) ? "Yes" : "No");
  73. }
  74.  
  75. /*****************************************************************************/
  76. /***     Scanning functions                                                ***/
  77. /*****************************************************************************/
  78.  
  79. void scanning_functions(void)
  80. {
  81.                                         /* Print a header.                   */
  82.                                         /* Print out the string we'll be     */
  83.                                         /*  scanning...                      */
  84.                                         /* Scan for first occurance of a char*/
  85.                                         /* Scan for first string found in the*/
  86.                                         /*  larger string.                   */
  87.                                         /* Scan for last occurance of a char.*/
  88.                                         /* Scan for first occurance of any of*/
  89.                                         /*  the characters in the substring. */
  90.                                         /*  NOTE: This function first looks  */
  91.                                         /*  for the first occurance of the   */
  92.                                         /*  first char in the substring.  If */
  93.                                         /*  that fails, it looks at the next */
  94.                                         /*  character in the substring...    */
  95.   printf("\nScanning functions:\n");
  96.   printf("-------------------\n");
  97.   printf("Using the string: %S\n", w_str1);
  98.   printf("First occurance of 's' is: %S\n", wcschr(w_str1, L's'));
  99.   printf("First occurance of ' is' is: %S\n", wcswcs(w_str1, L" is"));
  100.   printf("Last occurance of: 's' is: %S\n", wcsrchr(w_str1, L's'));
  101.   printf("Scan: '%S' for first occurance of: 'stuv': '%S'\n", w_str1,
  102.          wcspbrk(w_str1, L"stuv"));
  103. }
  104.  
  105. /*****************************************************************************/
  106. /***     Substring functions                                               ***/
  107. /*****************************************************************************/
  108.  
  109. void substring_functions(void)
  110. {
  111.                                         /* Print a header.                   */
  112.                                         /* Assign two wide strings.          */
  113.                                         /* Find out how many characters in   */
  114.                                         /*  the string are in the substring  */
  115.                                         /*  (starting from the first char)?  */
  116.                                         /* Find out how many characters in   */
  117.                                         /*  the string are NOT in the        */
  118.                                         /*  substring (starting from the     */
  119.                                         /*  first char)?                     */
  120.   printf("\nSubstring length functions:\n");
  121.   printf("---------------------------\n");
  122.   wcscpy(w_str1, L"cabbage");
  123.   wcscpy(w_str2, L"abc");
  124.   printf( "The first %d characters of \"%S\" are found in \"%S\"\n",
  125.        wcsspn(w_str1, w_str2), w_str1, w_str2);
  126.   wcscpy(w_str2, L"bde");
  127.   printf( "The first %d characters of \"%S\" are not found in \"%S\"\n",
  128.        wcscspn(w_str1, w_str2), w_str1, w_str2);
  129. }
  130.  
  131. /*****************************************************************************/
  132. /***     Tokenizing functions                                              ***/
  133. /*****************************************************************************/
  134.  
  135. void tokenizing_functions(void)
  136. {
  137.   int count;                            /* Loop counter.                     */
  138.  
  139.                                         /* Print a header.                   */
  140.                                         /* Assign a wide string.             */
  141.                                         /* Tokenize the string based on space*/
  142.                                         /*  comma and period characters, and */
  143.                                         /*  print out the token found.       */
  144.                                         /* Loop seven more times...          */
  145.                                         /*  Tokenize the string again, and   */
  146.                                         /*   print out the token.            */
  147.   printf("\nTokenizing functions:\n");
  148.   printf("---------------------\n");
  149.   wcscpy(w_str1, L"This line has spaces,commas,and periods.!");
  150.   printf("Tokenizing: '%S' based on: ' ,.'\n", w_str1);
  151.   printf("Token: %S\n", wcstok(w_str1, L" ,."));
  152.   for (count = 0; count < 7; count++)
  153.   {
  154.       printf("Token: %S\n", wcstok(NULL, L" ,."));
  155.   }
  156. }
  157.  
  158. /*****************************************************************************/
  159. /***     Main program                                                      ***/
  160. /*****************************************************************************/
  161.  
  162. void main(void)
  163. {
  164.                                         /* Perform each of the following     */
  165.                                         /*  groups of wide string functions. */
  166.   copy_concat_functions();
  167.   compare_functions();
  168.   scanning_functions();
  169.   substring_functions();
  170.   tokenizing_functions();
  171. }
  172.