home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
snipps97.zip
/
BASTRNGS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-07-05
|
1KB
|
43 lines
.I 0 1
/* +++Date last modified: 05-Jul-1997 */
.D 1 1
.I 158 24
/*
** BASIC INSTR$() work alike - returns position (1-based) of findstr in
** string, starting search at position start_pos (also 1-based).
**
** Function suggested by Tika Carr
*/
unsigned int instr(const unsigned int start_pos,
const char *string,
const char *findstr)
{
char *p;
unsigned int pos;
if (start_pos > strlen(string))
return 0;
else pos = start_pos - 1; /* BASIC offsets are one-based, not
zero-based as in C */
if (NULL != (p = strstr(string + pos, findstr)))
return (int)(p - (char *)string) + 1; /* Position 0 = position 1 */
else return 0;
}
.I 167 3
char *a = "This is a test", *b = "is" ,
*c = "\nSearching for \"%s\" in \"%s\" starting at position %d\n";
unsigned int i, pos;
.I 171 9
for (i = 2; i < strlen(a); i+= 2)
{
printf(c, b, a, i);
if (0 != (pos = instr(i, a, b)))
printf("Found at position %d\n", pos);
else puts("Not found");
}