inside the function mystrtok(s,’ ‘)
int i=0;
for(;input[i]!=’\0’;i++)
{
if(input[i]!=delim)
output[i]=input[i]
else
{
output[i]=’\0’;
input=input+i+1;
/here I didn’t understand this statement? How is i which is an integer added to a string? And why plus 1 is also added? How is it changing the input string/
return output;
}
//Also what did we do after above loop?and why?
output[i]=’\0’;
input=NULL:
return output;
Can you explain?
@isingh, actually there is one thing you need to know, that when you declare a array say
char a[100], then a is actually the address of first element of the array you can check it by printing (a==&a[0]) you will get 1 as output , so when you are doing input=input+1+i , then here input is not representing a string instead its representing a pointer , so when you add i+1 then index stores the address of index[i+1] .
What about when the statements written after the loop?What is that for?
output[i]=’\0’;
input=NULL:
these two statement will execute when we are at the last word in the sentence , so what happens is we inserted the last word in output inside the for loop since it was last word so we couldn’t execute the else statement so after the while loop we added output[i]=’\0’ to mark the end of word for output and input is set to null because since we have fetched the last word from input so input became empty ,so we set input as NULL
basically this program is replica of strtok function in cstring so, i will suggest to learn more about strtok() function in stl like what it does and what it returns it will clear your understanding and help you to understand above code
In case of any doubt feel free to ask 