Newbie question: Strings and string manipulation.
Author |
Message |
Erik Larse #1 / 13
|
 Newbie question: Strings and string manipulation.
I'm trying to get a substring of a string x bytes long, say from position X-n to position X-z. Other then treating the string as an array, and grabbing string[X-n] to string{X-z] and putting those bytes into another string one byte at a time, is there an easier way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ and burrowed through the manuals I have, but this seems like the only way to do it. -- ============================================================================== = Microsoft can have my modem when they take my cold, dead computer away. = = If it computes, TCP/IP it. If it can't TCP/IP, put a Un*x on it. = = If you can't afford a Un*x, get Linux. If it won't run Linux = = GET A REAL MACHINE! =
|
Thu, 21 Jan 1999 03:00:00 GMT |
|
 |
Erik Larse #2 / 13
|
 Newbie question: Strings and string manipulation.
I'm trying to get a substring of a string x bytes long, say from position X-n to position X-z. Other then treating the string as an array, and grabbing string[X-n] to string{X-z] and putting those bytes into another string one byte at a time, is there an easier way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ and burrowed through the manuals I have, but this seems like the only way to do it. -- ============================================================================== = Microsoft can have my modem when they take my cold, dead computer away. = = If it computes, TCP/IP it. If it can't TCP/IP, put a Un*x on it. = = If you can't afford a Un*x, get Linux. If it won't run Linux = = GET A REAL MACHINE! =
|
Thu, 21 Jan 1999 03:00:00 GMT |
|
 |
Chris Engebrets #3 / 13
|
 Newbie question: Strings and string manipulation.
|> I'm trying to get a substring of a string x bytes long, say from position X-n to |> position X-z. Other then treating the string as an array, and grabbing string[X-n] |> to string{X-z] and putting those bytes into another string one byte at a time, is |> there an easier way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ |> and burrowed through the manuals I have, but this seems like the only way to do |> it. Look at your documentation on strncpy(). Keep in mind that strncpy() does not null-terminate the "destination" string. A simple example: #include <stdio.h> #include <string.h> int main(void) { char src[] = "---foobar---"; char dest[4]; strncpy(dest, &src[3], 3); dest[3] = '\0'; printf("dest == %s\n", dest); return 0; Quote: }
This copies three characters from src into dest, starting with the fourth character in src. It then null-terminates the destination string, which is "foo." Regards, -- /*------------------------------------------------------------- Chris Engebretson // CSB - SED - Scientific Systems Development United States Geological Survey - National Mapping Division Mundt Federal Building, USGS EROS Data Center Sioux Falls, SD 57198 http://edcwww.cr.usgs.gov/eros-home.html
-------------------------------------------------------------*/
|
Fri, 22 Jan 1999 03:00:00 GMT |
|
 |
Gabor Egres #4 / 13
|
 Newbie question: Strings and string manipulation.
: I'm trying to get a substring of a string x bytes long, say from position X-n to : position X-z. Other then treating the string as an array, and grabbing string[X-n] : to string{X-z] and putting those bytes into another string one byte at a time, is : there an easier way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ : and burrowed through the manuals I have, but this seems like the only way to do : it. #include <string.h> #include <stdio.h> int main(void) { char line1[256]="hello world how I love thee"; char line2[256]; getsubstr(line1,line2,6,9); printf("%s\n",line2); return 0; Quote: }
char* getsubstr(char *str,char *substr,int start,int len) { strncpy(substr,str+6,len); *(substr + len) = 0; return substr; Quote: }
---------------------------------------------------------------------
Canada ---------------------------------------------------------------------
|
Fri, 22 Jan 1999 03:00:00 GMT |
|
 |
John F. Bod #5 / 13
|
 Newbie question: Strings and string manipulation.
Quote:
>I'm trying to get a substring of a string x bytes long, say from position X-n to >position X-z. Other then treating the string as an array, and grabbing string[X-n] >to string{X-z] and putting those bytes into another string one byte at a time, is >there an easier way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ >and burrowed through the manuals I have, but this seems like the only way to do >it.
Although there isn't a str* library function per se for this, you could use strncpy pretty much the same way. /* WARNING: The following is untested, off the top of my head. Caveat programmer */ #include <string.h> /* ** Procedure: jbsubstr ** Purpose: Copy a substring of src starting at byte start and len bytes long ** to dst. ** Parameters: src <-- input string to copy from ** dst --> string containing substring of src ** start <-- byte of src to start copying from ** len <-- number of bytes to copy ** Return Val: char * --> pointer to beginning of substring (same as dst) */ char *jbsubstr (const char *src, char *dst, int start, int len) { /* ** make sure start and len are valid. 0 <= start < strlen (src). ** 0 < len < strlen (src) - start */ if (0 <= start && start < (int) strlen (src)) { if (0 < len && len < ((int) strlen (src) - start)) { strncpy (dst, &src[start], len); dst[len] = '\0'; return dst; } else { /* length is either zero or exceeds end of input array. */ return NULL; } } else { /* start is either negative or past end of input array */ return NULL; } Quote: }
Anybody have a better way?
|
Sat, 23 Jan 1999 03:00:00 GMT |
|
 |
Lawrence Kirb #6 / 13
|
 Newbie question: Strings and string manipulation.
Quote: >char* >getsubstr(char *str,char *substr,int start,int len) >{ > strncpy(substr,str+6,len);
Did you mean str+start? Quote: > *(substr + len) = 0;
I agree with the FAQ and prefer: *substr = '\0'; strncat(substr,str+start,len); Since you wouldn't normally expect or require string functions to zero out trailing bytes in the destination array. Quote: -- -----------------------------------------
-----------------------------------------
|
Sat, 23 Jan 1999 03:00:00 GMT |
|
 |
Shai Inb #7 / 13
|
 Newbie question: Strings and string manipulation.
Quote:
> I'm trying to get a substring of a string x bytes long, say from position X-n to > position X-z. Other then treating the string as an array, and grabbing string[X-n] > to string{X-z] and putting those bytes into another string one byte at a time, is > there an easier way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ > and burrowed through the manuals I have, but this seems like the only way to do > it.
The simplest way of doing it is to assign the address of the start position in first string ( X-n) to a pointer of type char, then use either of the library functions strncpy or memcpy to copy a specified number of characters or bytes,from that pointer to the second string.
|
Sat, 23 Jan 1999 03:00:00 GMT |
|
 |
Ulrich Gehler #8 / 13
|
 Newbie question: Strings and string manipulation.
EL> I'm trying to get a substring of a string x bytes long, say from EL> position X-n to position X-z. Other then treating the string as an EL> array, and grabbing string[X-n] to string{X-z] and putting those EL> bytes into another string one byte at a time, is there an easier EL> way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ EL> and burrowed through the manuals I have, but this seems like the EL> only way to do it. Use the strncpy(char *dest, const char *src, size_t maxcnt) function. It copies at most `maxcnt' chars from src to dest; the rest (up to `maxcnt') chars in `dest' are padded with '\0' characters. Note that `dest' won't be zero terminated if exactly `maxcnt' bytes are copied. Example: #include <string.h> main(void) { /* 0123456789012345678901234567890 */ char str1[] = "International Business Machines"; char str2[9]; /* Exactly 8 chars will be copied */ strncpy(str2, str1 + 14, 8); /* Must zero terminate destination string */ str2[8] = 0; /* str2 now contains "Business" */ Quote: }
|
Sat, 23 Jan 1999 03:00:00 GMT |
|
 |
Gabor Egres #9 / 13
|
 Newbie question: Strings and string manipulation.
: >char* : >getsubstr(char *str,char *substr,int start,int len) : >{ : > strncpy(substr,str+6,len); : Did you mean str+start? Oops. Yes I did. I was just thinking about the string I had as a demonstration. :-) : > *(substr + len) = 0; : I agree with the FAQ and prefer: : *substr = '\0'; : strncat(substr,str+start,len); : Since you wouldn't normally expect or require string functions to zero out : trailing bytes in the destination array. : > : > return substr; : >} : -- : -----------------------------------------
: ----------------------------------------- -- ---------------------------------------------------------------------
Canada ---------------------------------------------------------------------
|
Sun, 24 Jan 1999 03:00:00 GMT |
|
 |
Bil #10 / 13
|
 Newbie question: Strings and string manipulation.
Quote:
> : I'm trying to get a substring of a string x bytes long, say from position X-n to > : position X-z. Other then treating the string as an array, and grabbing string[X-n] > : to string{X-z] and putting those bytes into another string one byte at a time, is > : there an easier way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ > : and burrowed through the manuals I have, but this seems like the only way to do > : it. > #include <string.h> > #include <stdio.h> > int > main(void) > { > char line1[256]="hello world how I love thee"; > char line2[256]; > getsubstr(line1,line2,6,9); > printf("%s\n",line2); > return 0; > } > char* > getsubstr(char *str,char *substr,int start,int len) > { > strncpy(substr,str+6,len);
I think you meant to use.... strncpy(substr,str+start,len); Quote: > *(substr + len) = 0;
and *(substr +len) = '\0'; Quote: I'm just being picky.....;) -- - Bill Cunningham Any day programming is better than fishing, sex, watersports, work, doing dishes, laundry, etc, etc. Famous quote (at 3:45 am before project deadline): "Hey that bug should only take a few 30 hour debugging sessions..."
|
Sun, 24 Jan 1999 03:00:00 GMT |
|
 |
Gabor Egres #11 / 13
|
 Newbie question: Strings and string manipulation.
: > : it. : > #include <string.h> : > #include <stdio.h> : > : > int : > main(void) : > { : > char line1[256]="hello world how I love thee"; : > char line2[256]; : > : > getsubstr(line1,line2,6,9); : > printf("%s\n",line2); : > return 0; : > } : > char* : > getsubstr(char *str,char *substr,int start,int len) : > { : > strncpy(substr,str+6,len); : I think you meant to use.... : strncpy(substr,str+start,len); Oops. Yes indeed I did. : > *(substr + len) = 0; : and : *(substr +len) = '\0'; No. I meant what I wrote. The two are equivalent. :-) : > : > return substr; : > } : > : I'm just being picky.....;) : -- : - Bill Cunningham : Any day programming is better than fishing, sex, watersports, work, doing dishes, laundry, etc, etc. : Famous quote (at 3:45 am before project deadline): : "Hey that bug should only take a few 30 hour debugging sessions..." -- ---------------------------------------------------------------------
Canada ---------------------------------------------------------------------
|
Mon, 25 Jan 1999 03:00:00 GMT |
|
 |
Eddie Bus #12 / 13
|
 Newbie question: Strings and string manipulation.
Quote:
> EL> I'm trying to get a substring of a string x bytes long, say from > EL> position X-n to position X-z. Other then treating the string as an > EL> array, and grabbing string[X-n] to string{X-z] and putting those > EL> bytes into another string one byte at a time, is there an easier > EL> way to do it? Similar to BASIC's (ugh) SUBSTR? I've read the FAQ > EL> and burrowed through the manuals I have, but this seems like the > EL> only way to do it. > Use the strncpy(char *dest, const char *src, size_t maxcnt) function. > It copies at most `maxcnt' chars from src to dest; the rest (up to > `maxcnt') chars in `dest' are padded with '\0' characters. > Note that `dest' won't be zero terminated if exactly `maxcnt' bytes > are copied. > Example: > #include <string.h> > main(void) > { > /* 0123456789012345678901234567890 */ > char str1[] = "International Business Machines"; > char str2[9]; > /* Exactly 8 chars will be copied */ > strncpy(str2, str1 + 14, 8); > /* Must zero terminate destination string */ > str2[8] = 0; > /* str2 now contains "Business" */ > }
That's the only way that I am aware of, and I've just looked it up in the K&R Book. There are no libraries or functions (under ANSI C) for manipulations of that sort. One of the Borland or (BLAH!) Microsoft packages probably has some sort of device in place for manipulations of that type though. Please, someone, correct me if I am wrong, but shouldn't you terminate with '\0' instead of 0? Also, I believe that, if you were to come up with a little arithmetic to tell how long the word you want to copy over was, you could make it of any length and still have it terminated properly. This would be done using a char * and a call to malloc. malloc needs the size of the 'string' (array of char*), so you need to do the arithmetic first, add one, copy the string to the newly created array and then terminate it. Once you determine the length of the new string, you should be able to copy the stuff from string1 to string2 very easily. I'm not going to elaborate on algorithms or code. Good Luck, Eddie
|
Wed, 03 Feb 1999 03:00:00 GMT |
|
 |
Lawrence Kirb #13 / 13
|
 Newbie question: Strings and string manipulation.
Quote:
>> #include <string.h> >> main(void) >> { >> /* 0123456789012345678901234567890 */ >> char str1[] = "International Business Machines"; >> char str2[9]; >> /* Exactly 8 chars will be copied */ >> strncpy(str2, str1 + 14, 8); >> /* Must zero terminate destination string */ >> str2[8] = 0; >> /* str2 now contains "Business" */ >> } >That's the only way that I am aware of,
Just look back through the thread, a couple of other possibilities have been suggested. Quote: > and I've just looked it up in the K&R > Book. There are no >libraries or functions (under ANSI C) for manipulations of that sort. One of > the Borland or (BLAH!) >Microsoft packages probably has some sort of device in place for manipulations > of that type though.
Wich you would hopefully avoid since such things would by definition be non-portable. Quote: >Please, someone, correct me if I am wrong, but shouldn't you terminate with > '\0' instead of 0?
'\0' and 0 are identical in that they both represent a constant with type int and value zero. '\0' is better style though. Quote: >Also, I believe that, if you were to come up with a little arithmetic to tell > how long the word you >want to copy over was, you could make it of any length and still have it > terminated properly. This >would be done using a char * and a call to malloc. malloc needs the size of > the 'string' (array of > char*), so you need to do the arithmetic first, add one, copy the string to > the newly created array >and then terminate it.
But you have to remember to free it when you've finished with it. -- -----------------------------------------
-----------------------------------------
|
Wed, 03 Feb 1999 03:00:00 GMT |
|
|
|