Issue with string tokenizer

char *myStrtok(char *input,char delim){

static char * ptr;
if(input!=NULL){
	ptr = input;
}
if(ptr==NULL){
	return NULL;
}


char* output = new char[strlen(ptr)+1];

int i;
for(i=0;ptr[i]!='\0';i++){
	if(ptr[i]==delim){
		output[i] = '\0';
		ptr = ptr + i + 1;
		return output;
	}
	output[i] = ptr[i];
}

output[i] = '\0';
ptr = NULL;
return output;

}
in this would the for loop not return each output but in strtok function we need only 1 output at a time

why have we used pointer her why not static char input = NULL;

why have we used pointer her why not static char *input = NULL;

HI @abhay170, no it will not because we have written here that
as soon as ptr[i] will be equal to delimiter then the output string will be returned. Once return statement is read and executed, the flow of control of program will go back to the parent function, and rest of loop will not be executed.

Hope this helps :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.