Groovy hepcat David Schwartz was jivin' on Fri, 05 Mar 2004 05:59:50
GMT in comp.lang.c.
string question's a cool scene! Dig it!
I have a string A, and I want to write a substring (of varying lengths)
starting at the beginning of A, to another string.
Can someone show me how to do that?
#include <stdio.h>
#include <assert.h>
#include <string.h>
/* copies 'len' characters from offset 'off' of string 'src' to 'dst'
returns dst (analogous to MID$ in BASIC) */
char *substr(char *dst, const char *src, int off, int len)
{
assert(NULL != dst);
assert(NULL != src);
assert(0 <= len);
assert(0 <= off);
sprintf(dst, "%.*s", len, src + off);
return dst;
}
/* copies rightmost 'len' characters from 'src' to 'dst'
returns dst (analogous to RIGHT$ in BASIC) */
char *rightstr(char *dst, const char *src, int len)
{
const char *p;
int sl;
assert(NULL != dst);
assert(NULL != src);
assert(0 <= len);
if(len >= (sl = strlen(src)))
p = src;
else
p = src + sl - len;
sprintf(dst, "%.*s", len, p);
return dst;
}
/* copies leftmost 'len' characters from 'src' to 'dst'
returns dst (analogous to LEFT$ in BASIC) */
char *leftstr(char *dst, const char *src, int len)
{
assert(NULL != dst);
assert(NULL != src);
assert(0 <= len);
sprintf(dst, "%.*s", len, src);
return dst;
}
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?