Test if an an array of pointers has some value in it - arrays

I basically want to test if char *tk[2]; has some value or not stored .
My programs has to do something when tk[0] and tk[1] both have values(a char stored in each of them) , but also do something else when only tk[0] is initialized with something and tk[1] is "empty".
This is my function where i initialize my array of pointers , that uses strtok.
The keywords can be something like "key1 key2" or "key1",so maximum 2 words.
void key(char *keywords[], char *tk[], int k) {
int p = 0;
char *delim = " \n";
int length = strlen(keywords[k]);
char buffer_key[length];
strcpy(buffer_key, keywords[k]);
char *token_key = strtok(buffer_key, delim);
while (token_key != NULL) {
tk[p++] = token_key;
token_key = strtok(NULL, delim);
}
}
and this is the block of code from my main function where i call the key function .
char *tk[2];
key(keywords, tk, k);
Next i want to check if only tk[0] has a char stored (so tk[1] is empty?).
How can i check this ?

You should initialize tk before calling the function. Then you can test if the values are filled in after it returns.
char *tk[2] = {0};
key(keywords, tk, k);
if (tk[0] && !tk[1]) {
// do something
}

Related

Splitting string arrays with delimiter

I'm using the following code to split a char array:
char* Split(char* e, int index) {
index = index -1;
char* v[index +2];
char *p;
int i = 0;
p = strtok(e, ",");
while(p && i < index +2)
{
v[i] = p;
p = strtok(NULL, ",");
i++;
};
// Serial.println(v[0]);
//Serial.println(v[1]);
// Serial.println(v[2]);
return v[index];
};
I'm calling the function like this:
char array[]="1,3,4,55,6,7,66";
Serial.println("array:");
Serial.println(array);
char *out;
out = Split(array,2);
Serial.println("out:");
Serial.println(out);
Serial.println("array:");
Serial.println(array);
out = Split(array,2);
Serial.println("out:");
Serial.println(out);
The first time I call the function, everythin is fine. The result I get is "3" , and that is what I expect.
But with the second call of the function, things goes crazy, and I get just some hieroglyphics.
When I check the variables with the Serial output, I can see that "array" is the second time just "1", and this might be the reason of the curious output of the function.
But I don't understand how the first call of the function can affect the value of "array", because this variable is not touched in the function.
Can anybody help me with clarifying this issue?
The output of the serial interface is lke this:
array:
1,3,4,55,6,7,66
out:
3
array:
1
out:
⸮}⸮a⸮⸮-:⸮⸮⸮m⸮⸮⸮⸮⸮⸮⸮]⸮ʻ⸮T⸮;⸮⸮⸮N}⸮⸮⸮⸮{R⸮U)⸮⸮⸮[G⸮⸮`j⸮⸮⸮⸮⸮v⸮⸮wz⸮⸮s⸮⸮⸮⸮⸮⸮}⸮⸮2⸮⸮vz~⸮⸮⸮⸮O}⸮⸮⸮/⸮⸮nv⸮⸮^j⸮yO⸮7{⸮⸮⸮⸮z⸮Z⸮⸮⸮⸮⸮⸮⸮7[⸮⸮⸮j⸮w⸮⸮⸮⸮⸮⸮⸮w)⸮⸮c⸮⸮}⸮⸮⸮⸮⸮⸮⸮⸮⸮v⸮⸮׺⸮m/V⸮ys<⸮⸮ٿ⸮⸮⸮׆⸮+>ֻ⸮z6⸮=⸮D⸮⸮⸮⸮~⸮⸮⸮⸮e⸮⸮?⸮=⸮⸮W⸮⸮⸮⸮}⸮e⸮ߣN绮⸮w⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮w⸮⸮⸮⸮?⸮⸮⸮⸮⸮⸮⸮Y⸮⸮޴f⸮v⸮⸮u⸮p?⸮⸮^h⸮⸮}⸮⸮ݼ⸮^Wo⸮⸮⸮⸮⸮_⸮⸮⸮⸮⸮⸮;s⸮⸮⸮⸮wZ⸮⸮⸮~⸮7⸮⸮⸮r⸮⸮⸮⸮)⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮f⸮⸮O⸮⸮⸮⸮⸮⸮⸮⸮⸮
⸮⸮7⸮⸮a.⸮⸮.kG⸮⸮8⸮⸮⸮⸮⸮⸮⸮⸮U⸮⸮⸮⸮⸮⸮⸮⸮⸮'⸮we⸮⸮⸮M⸮{⸮⸮Lu⸮no⸮⸮⸮>⸮⸮⸮⸮⸮⸮⸮~}⸮⸮⸮⸮⸮⸮⸮⸮y⸮⸮o⸮⸮⸮,'>}⸮⸮⸮+⸮X⸮⸮⸮/⸮⸮ױ⸮⸮⸮⸮̲⸮⸮-_M⸮⸮⸮⸮L~⸮#Φz~⸮⸮⸮⸮?⸮⸮⸮⸮⸮{/⸮_⸮:⸮jmc⸮m]S⸮_3⸮>o⸮׷⸮ݸv⸮⸮⸮|⸮
⸮⸮{_^⸮⸮o⸮?⸮⸮⸮⸮⸮⸮_⸮⸮⸮⸮{⸮⸮⸮^⸮⸮⸮⸮⸮⸮⸮⸮⸮ퟺ⸮⸮߿⸮⸮p⸮⸮⸮w?=⸮⸮⸮X⸮⸮⸮⸮⸮⸮_⸮oy⸮⸮M⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮w⸮⸮⸮[⸮⸮o⸮⸮⸮⸮޿7wE~⸮⸮⸮⸮⸮N⸮⸮o⸮x⸮=v/⸮⸮⸮⸮>⸮9⸮⸮ί⸮Y_Q⸮⸮l⸮⸮}'⸮⸮}⸮?⸮⸮ޭ⸮6⸮7⸮{⸮T⸮⸮⸮ ⸮r⸮⸮⸮⸮⸮⸮⸮
ܽ+'⸮⸮⸮⸮G⸮f⸮z⸮Gn⸮⸮n⸮/⸮⸮⸮⸮/⸮⸮⸮⸮Q⸮⸮⸮⸮⸮o⸮;⸮L⸮⸮r⸮⸮⸮⸮n/߿ſ⸮⸮⸮⸮q⸮⸮⸮ݮ⸮⸮⸮⸮⸮⸮+⸮⸮⸮⸮⸮⸮ﷹln?⸮⸮⸮⸮q⸮⸮⸮{⸮⸮⸮⸮⸮⸮⸮q⸮-⸮⸮{(⸮⸮f⸮⸮{⸮v⸮܀⸮oq⸮⸮⸮⸮⸮⸮߽⸮⸮nj⸮⸮⸮os⸮6۟g⸮⸮⸮⸮"⸮⸮7Z7⸮⸮yo⸮ӟ⸮⸮⸮w⸮⸮⸮⸮⸮⸮{⸮⸮⸮Vr⸮⸮]_⸮׷SS⸮_⸮w⸮⸮⸮wl⸮⸮⸮⸮P⸮⸮z⸮⸮m{⸮⸮⸮ݛs⸮(⸮⸮r⸮⸮˷:⸮%⸮⸮⸮⸮Z⸮⸮⸮m⸮W⸮⸮⸮⸮ם*⸮⸮⸮[>⸮⸮⸮/⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮?⸮⸮⸮⸮|⸮.⸮⸮⸮{⸮⸮⸮ꍥ⸮⸮⸮|⸮⸮⸮⸮⸮⸮⸮⸮⸮7~Ls⸮!⸮⸮⸮⸮⸮{⸮x⸮g⸮⸮|֍om~~⸮⸮⸮{⸮cϠ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮>;⸮W⸮⸮⸮⸮⸮⸮⸮⸮q⸮⸮[:⸮'⸮#⸮o⸮⸮_⸮uϾ⸮⸮⸮⸮[⸮⸮^⸮⸮⸮n⸮}⸮⸮⸮⸮⸮>/⸮_⸮⸮-⸮⸮⸮⸮s⸮⸮⸮}⸮⸮⸮⸮/⸮[w⸮⸮r⸮⸮_⸮⸮⸮⸮,⸮⸮ݯ⸮⸮⸮7ÿ⸮:⸮⸮⸮⸮Ί⸮⸮⸮⸮⸮t[|⸮⸮w⸮F⸮⸮⸮
Has anybody a better idea?
The solution depend on your data, if your data is always like a string with list of integers separated with ,, then it will probably better to split it into an int array, you can then write a generic split() function.
#define NUMBER_OF_ELEMENT 7
int splitted[NUMBER_OF_ELEMENT];
const char* delimiter = ",";
void split(const char *str, const char *delimiter) {
char temp[strlen(str)+1] = {0};
memcpy(temp, str, strlen(str));
int i=0;
char *p = strtok(temp, delimiter);
while(p != NULL) {
splitted[i++] = atoi(p);
p = strtok(NULL, delimiter);
}
}
int setup() {
char array[]="1,3,4,55,6,7,66";
Serial.begin(115200);
split(array, delimiter);
// get every splitted element as an int
for (int i=0; i<NUMBER_OF_ELEMENT, i++) {
Serial.print("Out:");
Serial.println(splitted[i]);
}
//if you really want to have string as result
Serial.println(String(splitted[3]));
}
OK I got it by making the array independent from change by strtok:
void setup(){
Serial.begin(115200);
char array[]="1,3,4,55,6,7,66";
char *out;
char array2[8];
strcpy(array2,array);
Serial.println("array2:");
Serial.println(array2);
out = Split(array2,2);
Serial.println("out:");
Serial.println(out);
Serial.println("array3:");
char array3[8];
strcpy(array3,array);
Serial.println(array3);
out = Split(array3,2);
Serial.println("out:");
Serial.println(out);
}
Not that elegant, but it works!!
Has anybody a better idea?
Thanks,great!
Yes my data is always int. So your function is perfect for me.
But for the sake of a clear structure, I would prefer to work with a return value, instead of changing the global variable "splitted".
Is that a good practice? Or do you prefer your variant for saving RAM?
Here my final solution for this task. The function just returns a single value, according to the given index:
int splitStrToInt(char *str, int index) {
//******************
//This function splits a string that is separated by commas and converts the values ​​to int,
//according to the given index
//caution!! The string may only consist of numerical values ​​!!
//Example: "1,3,4,55,6,7,66"
//******************
index--;
char e[strlen(str) + 1] = { 0 };
memcpy(e, str, strlen(str));
int v[index + 2];
char *p;
int i = 0;
p = strtok(e, ",");
while (p && i < index + 2) {
v[i] = atoi(p);
p = strtok(NULL, ",");
i++;
}
;
return v[index];
}

Replace underscore with white spaces and make the first letter of the name and the surname to upper case

i want to replace _ (underscore) with white spaces and make the first letter of the name and the surname to upper case while printing the nameList in searchKeyword method.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void searchKeyword(const char * nameList[], int n, const char keyword[])
{
int i,name=0;
char *str;
const char s[2] = " " ;
for(i=0;i<n;i++)
{
char *str = (char *) malloc((strlen(nameList[0])+1)*sizeof(char));
strcpy(str,nameList[i]);
strtok(str,"_");
if(strcmp(keyword,strtok(NULL,"_"))==0) // argument NULL will start string
{ // from last point of previous string
name++;
if(nameList[i] == '_')
strcpy(nameList[i],s);
//nameList[i] = ' ';
printf("%s\n",nameList[i]);
}
}
if(name==0)
{
printf("No such keyword found\n");
}
free(str); //deallocating space
}
int main()
{
char p1[] = "zoe_bale";
char p2[] = "sam_rodriguez";
char p3[] = "jack_alonso";
char p4[] = "david_studi";
char p5[] = "denzel_feldman";
char p6[] = "james_bale";
char p7[] = "james_willis";
char p8[] = "michael_james";
char p9[] = "dustin_bale";
const char * nameList[9] = {p1, p2, p3, p4, p5, p6, p7, p8, p9};
char keyword[100];
printf("Enter a keyword: ");
scanf("%s", keyword);
printf("\n");
searchKeyword(nameList, 9, keyword);
printf("\n");
for (int i = 0; i < 9; i++)
printf("%s\n",nameList[i]);
return 0;
}
Search through the strings and print the ones whose surname part is equal to keyword.
As shown in the example runs below, the strings are printed in “Name Surname” format (the first letters are capitalized).
Output should be like this:
Enter a keyword: james
Michael James
zoe_bale
sam_rodriguez
jack_alonso
david_studi
denzel_feldman
james_bale
james_willis
michael_james
dustin_bale
There is no reason to dynamically allocate storage for your name and surname. Looking at your input, neither will exceed 9-characters, so simply using an array for each of 64-chars provides 6X the storage required (if you are unsure, double that to 128-chars and have 1200% additional space). That avoids the comparatively expensive calls to malloc.
To check whether keyword exists in nameList[i], you don't need to separate the values first and then compare. Simply use strstr (nameList[i], keyword) to determine if keyword is contained in nameList[i]. If you then want to match only the name or surname you can compare again after they are separated. (up to you)
To parse the names from the nameList[i] string, all you need is a single pointer to locate the '_' character. A simple call to strchr() will do and it does not modify nameList[i] so there is no need to duplicate.
After using strchr() to locate the '_' character, simply memcpy() from the start of nameList[i] to your pointer to your name array, increment the pointer and then strcpy() from p to surname. Now you have separated name and surname, simply call toupper() on the first character of each and then output the names separate by a space, e.g.
...
#include <ctype.h>
#define NLEN 64
void searchKeyword (const char *nameList[], int n, const char keyword[])
{
for (int i = 0; i < n; i++) { /* loop over each name in list */
if (strstr (nameList[i], keyword)) { /* does name contain keyword? */
char name[NLEN], surname[NLEN]; /* storage for name, surname */
const char *p = nameList[i]; /* pointer to parse nameList[i] */
if ((p = strchr(p, '_'))) { /* find '_' in nameList[i] */
/* copy first-name to name */
memcpy (name, nameList[i], p - nameList[i]);
name[p++ - nameList[i]] = 0; /* nul-terminate first name */
*name = toupper (*name); /* convert 1st char to uppwer */
/* copy last name to surname */
strcpy (surname, p);
*surname = toupper (*surname); /* convert 1st char to upper */
printf ("%s %s\n", name, surname); /* output "Name Surname" */
}
}
}
}
Example Use/Output
Used with the remainder of your code, searching for "james" locates those names containing "james" and provides what looks like the output you requested, e.g.
$ ./bin/keyword_surname
Enter a keyword: james
James Bale
James Willis
Michael James
zoe_bale
sam_rodriguez
jack_alonso
david_studi
denzel_feldman
james_bale
james_willis
michael_james
dustin_bale
(note: to match only the name or surname add an additional strcmp before the call to printf to determine which you want to output)
Notes On Your Existing Code
Additional notes continuing from the comments on your existing code,
char *str = (char *) malloc((strlen(nameList[0])+1)*sizeof(char));
should simply be
str = malloc (strlen (nameList[i]) + 1);
You have previously declared char *str; so the declaration before your call to malloc() shadows your previous declaration. If you are using gcc/clang, you can add -Wshadow to your compile string to ensure you are warned of shadowed variables. (they can have dire consequences in other circumstances)
Next, sizeof (char) is always 1 and should be omitted from your size calculation. There is no need to cast the return of malloc() in C. See: Do I cast the result of malloc?
Your comparison if (nameList[i] == '_') is a comparison between a pointer and integer and will not work. Your compiler should be issuing a diagnostic telling you that is incorrect (do not ignore compiler warnings -- do not accept code until it compiles without warning)
Look things over and let me know if you have further questions.
that worked for me and has no memory leaks.
void searchKeyword(const char * nameList[], int n, const char keyword[])
{
int found = 0;
const char delim = '_';
for (int i = 0; i < n; i++) {
const char *fst = nameList[i];
for (const char *tmp = fst; *tmp != '\0'; tmp++) {
if (*tmp == delim) {
const char *snd = tmp + 1;
int fst_length = (snd - fst) / sizeof(char) - 1;
int snd_length = strlen(fst) - fst_length - 1;
if (strncmp(fst, keyword, fst_length) == 0 ||
strncmp(snd, keyword, snd_length) == 0) {
found = 1;
printf("%c%.*s %c%s\n",
fst[0]-32, fst_length-1, fst+1,
snd[0]-32, snd+1);
}
break;
}
}
}
if (!found)
puts("No such keyword found");
}
hopefully it's fine for you too, although I use string.h-functions very rarely.

Get part of a string with dynamic length in C

I got the following string from the user:
char *abc = "a234bc567d";
but all the numbers can have different lengths than in this example (letters are constants).
How can I get each part of numbers? (again, it can be 234 or 23743 or something else..)
I tried to use strchr and strncpy but I need to allocate memory for this (for strncpy), and I hope there is a better solution.
Thanks.
You can do something like this:
char *abc = "a234bc567d";
char *ptr = abc; // point to start of abc
// While not at the end of the string
while (*ptr != '\0')
{
// If position is the start of a number
if (isdigit(*ptr))
{
// Get value (assuming base 10), store end position of number in ptr
int value = strtol(ptr, &ptr, 10);
printf("Found value %d\n", value);
}
else
{
ptr++; // Increase pointer
}
}
If I understand your question, you are trying to extract the parts of the user input that contain numbers ... and the sequence of numbers can be variable ... but the letters are fixed i.e. a or b or c or d. Correct ... ? The following program may help you. I tried it for strings "a234bc567d", "a23743bc567d" and "a23743bc5672344d". Works ...
int main()
{
char *sUser = "a234bc567d";
//char *sUser = "a23743bc567d";
//char *sUser = "a23743bc5672344d";
int iLen = strlen(sUser);
char *sInput = (char *)malloc((iLen+1) * sizeof(char));
strcpy(sInput, sUser);
char *sSeparator = "abcd";
char *pToken = strtok(sInput, sSeparator);
while(1)
{
if(pToken == NULL)
break;
printf("Token = %s\n", pToken);
pToken = strtok(NULL, sSeparator);
}
return 0;
}

Getting incorrect values when accessing variables passed along in a pointer to a character array for strtok

Here is my code
//Split up the config by lines
int x;
int numberOfConfigLines = 0;
for (x = 0; x < strlen(buffer); x++)
{
if (buffer[x] == '\n') {
numberOfConfigLines++;
}
}
char *configLines[numberOfConfigLines];
tokenize(configLines, buffer, "\n", numberOfConfigLines);
The idea of this function is to count the amount of newlines in a buffer, then split the buffer into a strtok array using this:
#include <string.h>
#include <stdlib.h>
void tokenize(char **arrToStoreTokens, char *delimitedString, char *delimiter, int expectedTokenArraySize) {
//Create a clone of the original string to prevent making permanent changes to it
char *tempString = (char *)malloc(strlen(delimitedString) + 1);
strcpy(tempString, delimitedString);
if (expectedTokenArraySize >= 1) {
arrToStoreTokens[0] = strtok(tempString, delimiter);
int x;
for (x = 1; x < expectedTokenArraySize; x++ ) {
arrToStoreTokens[x] = strtok(NULL, delimiter);
}
}
//Dispose of temporary clone
free(tempString);
}
If I access arrToStoreTokens[0] directly, I get the correct result, however when I try to access configLines[0] once thetokenize function has ended, I get different results (can be unknown characters or simply empty)
Additionally, I believe this has only started occurring once I began running the program as root (for a different requirement) - I may be wrong though. - EDIT: Confirmed not to be the problem.
Any ideas?
strtok doesn't reallocate anything. It only makes cut and pointers of what you gave to it.
Your array stores pointers that strtok gives you, but don't copy contents.
So if you free your tempString variable, you free data that was pointed by return values of strtok. You have to keep it and free it only at the end.
Or you can make a strdup of each return of strtok to store it in your array to make a real copy of each token, but in this case, you shall have to free each token at the end.
The second solution would looks like this :
void tokenize(char **arrToStoreTokens, char *delimitedString, char *delimiter, int expectedTokenArraySize) {
//Create a clone of the original string to prevent making permanent changes to it
char *tempString = (char *)malloc(strlen(delimitedString) + 1);
strcpy(tempString, delimitedString);
if (expectedTokenArraySize >= 1) {
arrToStoreTokens[0] = strdup(strtok(tempString, delimiter)); // Here is the new part : strdup
int x;
for (x = 1; x < expectedTokenArraySize; x++ ) {
arrToStoreTokens[x] = strdup(strtok(NULL, delimiter)); // Here is the new part : strdup
}
}
//Dispose of temporary clone
free(tempString);
}
And after use of this array, you shall have to delete it, with a function like this :
void deleteTokens(char **arrToStoreTokens, int arraySize)
{
int x;
for (x = 0; x < arraySize; ++x)
{
free(arrToStoreTokens[x]);
}
}

Learning c getting position pointer out of strtok as int

Beginner here,
somewhat confused about an exercise:
Tutorial Last one on the page (it is german). I should read HTML-Lines and print attributes and their values. The declaration of the function which should be used is given.
Two things irritate me:
1. The Line is stored in a const char string, but i would like the User to type in his desired HTML-line. It seems not to be possible to change a const variable at runtime. How can it be achieved without changing the given declaration?
2. The Tutorial wants me to give back the position of strtok-search as an integer, but I read online that this value is stored within strtok, is there a way to cast that, or get it somehow?
To solve the exercise I wrote this code, but the program crashes at runtime with "Segmentation fault (core dumped)"-Message and I don't know why, could someone please explain that to me? (I probably need malloc, but for which variable?)
//cHowTo Uebung Teil 2 Nr. 4
//HTMLine.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//char getHTMLline ();
int getHtmlAttributes(const char *string, int start, char *attrNamem,char *attrValue); //given by Tutorial
int main(int argc, char *argv) //because i want user-input later on, if possible
{
const char strg[]= {"<img src=\"kurt.jpg\" width=\"250\" alt=\"Kurt Kanns\" />"}; //given example line by tutorial
char attriN[255]={0}, attriV[255]={0};
int pos=99;
//printf("Please type the tag for analysis.\n");
//fgets(strg, 255, stdin);
printf("attribute\tvalue\n\n");
do
{
pos = getHtmlAttributes(strg, pos, attriN, attriV); //pos should be strtok-search-position
printf("%s\t\t%s\n", attriN, attriV);
}
while(pos!=1);
return EXIT_SUCCESS;
}
int getHtmlAttributes(const char *string, int start, char *attrNamem, char *attrValue)
{
int i, len;
char *ptr;
len = strlen(string);
char stringT[len]; //variable used to be split by strtok
for(i=0; i<len; i++)
stringT[i]=string[i];//copy string to stringT
if(start==99)
ptr = strtok(stringT, "<="); //get first attribute as whole
else
ptr = strtok(NULL, "= "); // get following attributes
for(i=0; i<len; i++)
attrNamem[i] = ptr[i];
ptr = strtok(NULL, "\""); //get values
for(i=0; i<len; i++)
attrValue[i] = ptr[i];
if(ptr == NULL) //if search complete
{
return 1;
}
else // if search continues
{
return 0;
}
}
//char getHTMLline ()
//{
// char user_input;
// scanf("%s", &user_input);
// return user_input;
//}
What strtok() does is that if you call it with a string different to NULL it stores a pointer to that string internally and returns the first token. A subsequent call with NULL then uses that internally stored pointer to determine the next token.
Now what happens in your code is:
When you call getHtmlAttributes() for the first time you create a copy of the given string in stringT and pass that copy to strtok(). The next time you call strtok( NULL, ... ). And there we have two bugs:
your loop to copy string() to stringT() is not correct. You don't copy
the teminating '\0'. Just use strcpy() in such cases
the important one: When you call getHtmlAttributes() for the second time, you call strtok( NULL, ... ) but the lifetime of the stringT that it has been called with originally has ended with the first call of getHtmlAttributes() returning, because stringT is a local variable that is created anew on the stack every time the function is called. You could solve that problem by either
declaring static char stringT[N] where N must be a constant (like 255), you can't use len (what should have been len+1 anyway) in that case
creating a dynamically allocated copy of string by char *stringT = strdup( string );. Please do that only if you call strtok( stringT, ... ) aferwards and be aware that without additional code you have a memory leak because you are not able to free that memory again.
what I would pefer: use string directly instead of stringT. In that case you should not declare string as const and create a copy of strg in main() that you pass to the function
Edit
I think the request "give back the position of strtok-search as an integer" means, you should return the offset of the found token in the complete string. That's quite easy to achieve, if you use the suggested solution static char stringT[N] from above:
As strtok() works on the string passed by the first call, after receiving a ptr different from NULL, you can calculate and return int offset = ptr - stringT;
Edit 2
And now something completely different
I've just read the Tutorial you have linked to (greetings from Hessen :-) ) and I think the idea is to use a new strtok() loop every time the function is called. That could like this:
int getHtmlAttributes(const char *string, int start, char *attrName, char *attrValue)
{
char *buf;
char *attrptr;
char *ptr;
// copy the substring starting at start to buf
// you should add error checking (buf may become NULL)
buf = strdup( string+start );
// first step: tokenize on whitespace; we stop at the first token that contains a "="
for( attrptr = strtok( buf, " \t" ); attrptr && (ptr = strchr( attrptr, '=' )) != NULL; attrptr = strtok( NULL, " \t" ) ) ;
if( attrptr ) {
// copy everything before "=" to attrName
sprintf( attrName, "%.*s", ptr-attrptr, attrptr );
// copy everything after "=" to attrValue
strcpy( attrValue, ptr+1 );
// the next start is the current start + the offset of the attr found
// + the length of the complete attr
start += (attrptr - buf) + strlen( attrptr );
free( buf );
return start;
} else {
// no more attribute
free( buf );
return -1;
}
}

Resources