home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!cs.utexas.edu!asuvax!ennews!envmsa.eas.asu.edu!ptran
- From: ptran@envmsa.eas.asu.edu (Phi-Long Tran)
- Subject: Re: Checking variables for characters & Copying portions of strings
- Message-ID: <28JUL199221222079@envmsa.eas.asu.edu>
- News-Software: VAX/VMS VNEWS 1.4-b1
- Sender: news@ennews.eas.asu.edu (USENET News System)
- Organization: Arizona State University, Tempe, AZ
- Date: Wed, 29 Jul 1992 04:22:00 GMT
- Lines: 78
-
- In article <1992Jul27.151345.3124@vpnet.chi.il.us>,
- mox@vpnet.chi.il.us (William Moxley) writes...
-
- > I'm adding an editing feature to a little database I wrote. I need to be
- > able to see if a certain variable has anything but whitespace in it. (So
- > that you can just hit enter past some of the fields instead of having to
- > retype everything) How can I do this? I've tried strlen and sizeof, but
- > they just tell me how big the string is defined for.
-
- If all you want to do is determine if a string contains "whitespace",
- try the following function. It returns the location of the first non-white
- character in its argument. It calls "isspace" to determine if a character
- is "whitespace". The return value may be useful for later processing. The
- check for NULL is for safety reasons, such as when the caller is passing
- function first_nonwhite the return value of another function, passing on
- the responsibility of checking for NULL to the caller. Using the routine,
- a string is non-white if the return value is not NULL and does not point to
- the null terminator.
-
- char * str_firstnonwhite( const char *string )
- {
- if (string == NULL) return NULL;
- while ((*string != '\0') && isspace(*string)) string++;
- return string;
- }
-
- > Also, an unrelated question. How can you copy a portion of a string into
- > another? What I want to do is to take a part out of the middle section
- > of string 1, and put it in the middle section of string 2. I've looked
- > at strncpy, but I don't know if/how it's possible to make it start from
- > the middle of a string.
-
- It sounds like you want to insert one string into another, such as
- inserting the string "Riding " into "Little Red Hood" at offset 11 to
- produce "Litte Red Riding Hood". First, we need to "shift" the destination
- over by +n bytes, where n is the length of the string to insert (if n is 0,
- we just call strcpy). When we shift the destination string, we copy from
- the null terminator of the string to the first character. This avoids
- overlapping problems. After shifting, we copy the string to insert "into"
- the destination string.
-
- char * str_insert( char * pdest, const char * pinsert )
- {
- int insertlen;
- int idest;
-
- if (pdest == NULL) return NULL;
- if ((pinsert == NULL) || (*pinsert == '\0')) return pdest;
-
- if (*pdest == '\0') return strcpy(pdest, pinsert);
-
- insertlen = strlen(pinsert);
- for (idest = strlen(pdest); idest >= 0; idest --)
- pdest[idest + insertlen] = pdest[idest];
- return strncpy(pdest, pinsert, insertlen);
- }
-
- Example:
-
- char test_buffer[40];
-
- strcpy(test_buffer, "Little Red Hood");
- str_insert(&test_buffer[11], "Riding ");
-
- @ = null terminator.
- 1 1 2 2 3
- 0....5....0....5....0....5....0
- Before: test_buffer Little Red Hood@
- After: test_buffer Little Red Riding Hood@
-
- This might not be exactly what you want, but maybe it'll give you some
- ideas. Notice that the routine assumes there is enough room to insert
- pinsert.
-
- ---
- Phi-Long Tran
- ptran@asuvax.eas.asu.edu
- Arizona State University
-