@ledow
Code:
short int string_parser(char *userstring, char *target, char magic, short int word)
{
static char subject[PARSE_SIZE_LIMIT]; subject[0] = '\0';
char *w = &magic;//assign the address of magic to w
char a = 0,b = 0,c = 0,x = 0,z = 0;//temp variables
short int length = 0,orig = 0;// get the length of the string
short int offset = 0,y = 1;//segment control
char *p;//used for assigning pointer address returned by strtok()
//** format subject array of 'natural' garbage with spaces & reset x **
//for (x = 0;x < PARSE_SIZE_LIMIT; x++) { subject[x] = ' '; } x = 0;
orig = strlen(userstring);//get size length target array
//copy and reformat the userstring into, 0 - PARSE_SIZE_LIMIT ; format.
//for lines larger or equal than the size limit....
if (orig >= (PARSE_SIZE_LIMIT - 1))
{
for (x = 0; x <= (PARSE_SIZE_LIMIT - 1); x++)
{ subject[x] = userstring[x]; }
}
//for lines less than the size limit....
if (orig < (PARSE_SIZE_LIMIT - 1))
{
for (x = 0; x <= orig; x++)
{ subject[x] = userstring[x]; }
}
subject[x+1] = '\0';//add terminating null character
//if (word == -1) { printf("<%s> ",subject); }//temp - keep
length = strlen(subject);//get string length of subject[];
//count where all the "magic" characters are
for(x = 0;x < length;x++)
{
//if the subject character = "magic" character then count it
if(subject[x] == magic)
{
a = x;//set a to x , to match where x is currently at.
// check for no characters between "magic" characters if x > 0
// if x = 0 then do nothing....
if(x > 0)
{
c = a - b;// c = the new count (of x) - the old count (of x)
/*
a magic character must have a difference between it's last
position (if there is one) and its new position ( which is greater
than 1 ) to be counted as magic character. Therefore, if "#" is a
magic character and a string is, "##magic:" it would be discounted.
However, a string such as,"# #magic:" would be counted as two
magic characters
*/
if(c > 1) { y++; } /* if c > 1 then add 1 to y */
}
b = a;//set b to a , to save the previous count.
}
}
//if the original length of the userstring is (PARSE_SIZE_LIMIT - 1)
//then subtract 1
if (orig >= (PARSE_SIZE_LIMIT - 1)) { y -= 1; }
//the value of word finalizes the work of string_parser
//return the number parts in the string
if(word == -1) { return y; }
//return the modified user string
if(word == 0) { strcpy(target,subject); }
//** this is where strtok() is used **
//copy the first word in the string (must be done to find the next words)
if(word == 1)
{
p = strtok(subject,w);
strcpy(target,p);
}
//copy all the next words (sequencially) in the string
if(word > 1)
{
p = strtok(NULL,w);
strcpy(target,p);
}
return 0;
}