home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / sqdev200.zip / samples / 1stchar.c < prev    next >
C/C++ Source or Header  |  1994-05-23  |  2KB  |  88 lines

  1. /*# name=First-character-of-word-in-string function
  2. */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "prog.h"
  7.  
  8. /*
  9. main()
  10. {
  11.   char *test="  NORMAL   ";
  12.   char *test2="NORMAL\n";
  13.   char *test3="Sysop";
  14.  
  15.   printf("1:`%s'\n",firstchar(test," \t\n",2));
  16.   printf("2:`%s'\n",firstchar(test2," \t\n",2));
  17.   printf("3:`%s'\n",firstchar(test3," \t\n",2));
  18. }
  19. */
  20.  
  21. char * _fast firstchar(char *strng,char *delim,int findword)
  22. {
  23.   int x,
  24.       isw,
  25.       sl_d,
  26.       sl_s,
  27.       wordno=0;
  28.  
  29.   char *string,
  30.        *oldstring;
  31.  
  32.   /* We can't do *anything* if the string is blank... */
  33.  
  34.   if (! *strng)
  35.     return NULL;
  36.  
  37.   string=oldstring=strng;
  38.  
  39.   sl_d=strlen(delim);
  40.  
  41.   for (string=strng;*string;string++)
  42.   {
  43.     for (x=0,isw=0;x <= sl_d;x++)
  44.       if (*string==delim[x])
  45.         isw=1;
  46.  
  47.     if (isw==0)
  48.     {
  49.       oldstring=string;
  50.       break;
  51.     }
  52.   }
  53.  
  54.   sl_s=strlen(string);
  55.  
  56.   for (wordno=0;(string-oldstring) < sl_s;string++)
  57.   {
  58.     for (x=0,isw=0;x <= sl_d;x++)
  59.       if (*string==delim[x])
  60.       {
  61.         isw=1;
  62.         break;
  63.       }
  64.  
  65.     if (!isw && string==oldstring)
  66.       wordno++;
  67.  
  68.     if (isw && (string != oldstring))
  69.     {
  70.       for (x=0,isw=0;x <= sl_d;x++) if (*(string+1)==delim[x])
  71.       {
  72.         isw=1;
  73.         break;
  74.       }
  75.  
  76.       if (isw==0)
  77.         wordno++;
  78.     }
  79.  
  80.     if (wordno==findword)
  81.       return((string==oldstring || string==oldstring+sl_s) ? string : string+1);
  82.   }
  83.  
  84.   return NULL;
  85. }
  86.  
  87.  
  88.