| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | LEFT$ |
BB> The untested code does work, but could be a bit tricky to
BB> use because of the Pascal type strings.
You should look in SNIPPETS!
In the meantime, have a look at this:
/*_|_| BAS_STR.C
_|_|_| Simple string handling techniques using sprintf().
_|_|_| No warrantees or gurantees are given or implied.
_|_|_| Released to PUBLIC DOMAIN by Kurt Kuzba. (7/3/98)*/
#include
#include
#include
char* left_s(char *S, int len)
{
char *left = malloc(len + 1);
if(left)
{
if(S && *S)
{
memmove(left, S, len);
left[len] = (char)'\0';
}
else
{
free(left);
left = NULL;
}
}
return left;
}
char* right_s(char *S, int len)
{
char *right = NULL;
if(S && *S)
{
int Slen = strlen(S);
len = (Slen < len) ? Slen : len;
right = malloc(len + 1);
if(right)
{
memmove(right, S + Slen - len, len);
right[len] = (char)'\0';
}
}
return right;
}
char* mid_s(char *S, int start, int len)
{
char *mid = NULL;
if(S && *S)
{
int Slen = strlen(S);
len = (len > (Slen - start) || 0 == len) ? Slen - start : len;
mid = malloc(len + 1);
if(mid)
{
memmove(mid, S + start, len);
mid[len] = (char)'\0';
}
}
return mid;
}
int main(void)
{
char original[] = "* May 19 12:34:00", *S_mid, *S_left, *S_right;
puts("\n[][] Examples of BASIC-type string handling in C. [][]");
printf("\n\"%s\" <= Original string.\n", original);
/* example of using left_s() */
S_left = left_s(original, 8);
printf("\n\"%s\" <= left(8)\n", S_left);
free(S_left);
/* example of using right_s() */
S_right = right_s(original, 8);
printf("\n\"%s\" <= right(8)\n", S_right);
free(S_right);
/* example of using mid_s(), length defined */
S_mid = mid_s(original, 3, 12);
printf("\n\"%s\" <= mid(3, 12)\n", S_mid);
free(S_mid);
/* example of using mid_s(), length undefined */
S_mid = mid_s(original, 10, 0);
printf("\n\"%s\" <= mid(10, 0)\n", S_mid);
free(S_mid);
return 0;
}
/*_|_| end BAS_STR.C */
---
> ] * Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728 SEEN-BY: 639/252 670/213 218 @PATH: 154/750 222 396/1 633/260 635/506 728 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.