home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11689 < prev    next >
Encoding:
Text File  |  1992-07-28  |  3.5 KB  |  90 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!cs.utexas.edu!asuvax!ennews!envmsa.eas.asu.edu!ptran
  3. From: ptran@envmsa.eas.asu.edu (Phi-Long Tran)
  4. Subject: Re: Checking variables for characters & Copying portions of strings
  5. Message-ID: <28JUL199221222079@envmsa.eas.asu.edu>
  6. News-Software: VAX/VMS VNEWS 1.4-b1  
  7. Sender: news@ennews.eas.asu.edu (USENET News System)
  8. Organization: Arizona State University, Tempe, AZ
  9. Date: Wed, 29 Jul 1992 04:22:00 GMT
  10. Lines: 78
  11.  
  12. In article <1992Jul27.151345.3124@vpnet.chi.il.us>, 
  13. mox@vpnet.chi.il.us (William Moxley) writes...
  14.  
  15. > I'm adding an editing feature to a little database I wrote.  I need to be
  16. > able to see if a certain variable has anything but whitespace in it.  (So
  17. > that you can just hit enter past some of the fields instead of having to
  18. > retype everything) How can I do this?  I've tried strlen and sizeof, but
  19. > they just tell me how big the string is defined for.
  20.  
  21.      If all you want to do is determine if a string contains "whitespace",
  22. try the following function.  It returns the location of the first non-white
  23. character in its argument.  It calls "isspace" to determine if a character
  24. is "whitespace".  The return value may be useful for later processing.  The
  25. check for NULL is for safety reasons, such as when the caller is passing
  26. function first_nonwhite the return value of another function, passing on
  27. the responsibility of checking for NULL to the caller.  Using the routine,
  28. a string is non-white if the return value is not NULL and does not point to
  29. the null terminator.
  30.  
  31. char * str_firstnonwhite( const char *string )
  32. {
  33.    if (string == NULL) return NULL;
  34.    while ((*string != '\0') && isspace(*string)) string++;
  35.    return string;
  36. }
  37.  
  38. > Also, an unrelated question.  How can you copy a portion of a string into
  39. > another?  What I want to do is to take a part out of the middle section
  40. > of string 1, and put it in the middle section of string 2.  I've looked
  41. > at strncpy, but I don't know if/how it's possible to make it start from
  42. > the middle of a string.
  43.  
  44.      It sounds like you want to insert one string into another, such as
  45. inserting the string "Riding " into "Little Red Hood" at offset 11 to
  46. produce "Litte Red Riding Hood".  First, we need to "shift" the destination
  47. over by +n bytes, where n is the length of the string to insert (if n is 0,
  48. we just call strcpy).  When we shift the destination string, we copy from
  49. the null terminator of the string to the first character.  This avoids
  50. overlapping problems.  After shifting, we copy the string to insert "into"
  51. the destination string.
  52.  
  53. char * str_insert( char * pdest, const char * pinsert )
  54. {
  55.    int insertlen;
  56.    int idest;
  57.  
  58.    if (pdest == NULL) return NULL;
  59.    if ((pinsert == NULL) || (*pinsert == '\0')) return pdest;
  60.  
  61.    if (*pdest == '\0') return strcpy(pdest, pinsert);
  62.  
  63.    insertlen = strlen(pinsert);
  64.    for (idest = strlen(pdest); idest >= 0; idest --) 
  65.       pdest[idest + insertlen] = pdest[idest];
  66.    return strncpy(pdest, pinsert, insertlen);
  67. }
  68.  
  69. Example:
  70.  
  71.      char test_buffer[40];
  72.  
  73.      strcpy(test_buffer, "Little Red Hood");
  74.      str_insert(&test_buffer[11], "Riding ");
  75.  
  76.      @ = null terminator.
  77.                     1    1    2    2    3
  78.               0....5....0....5....0....5....0
  79.      Before:  test_buffer Little Red Hood@
  80.      After:   test_buffer Little Red Riding Hood@
  81.  
  82.      This might not be exactly what you want, but maybe it'll give you some
  83. ideas.  Notice that the routine assumes there is enough room to insert
  84. pinsert.
  85.  
  86. ---
  87. Phi-Long Tran
  88. ptran@asuvax.eas.asu.edu
  89. Arizona State University
  90.