here how has sir used while(key>1) and inside it doing key–, suppose in 10 20 30 40 i have enter the key 4 , then how it is returning 40 ?, because of return statement is outside the loop so it should return 20 naa instead of 40?? please clear my doubt…
String challenge using strtok() function
Are you talking about this segment of code??
string extract_string_at_key(string str,int key)
{
char *s;
s=strtok((char*)str.c_str()," ");
while(key>1)
{
s=strtok(NULL," ");
key--;
}
return (string)s;
}
i am considering your input
10 20 30 40 and key is 4
so intially s contains 10
after that we enter into loop and s become 20 and key=3
then s = 30 key=2
then s= 40 key =1
now we can’t go inside loop and come out of the loop
and return 40 as string
i hope it is clear now
if yes show your response with
and don’t forgot to mark doubt as resolved
if you want to ask something about this feel free to ask
you have cleared my previous doubt, thanks u for that
… But i have one more doubt that in strtok() function why we are using char *s , can we use only char s? if not then what is the difference between char s are char * s??
char s is a character named s
while
char * s is pointer to char named s
char * s can also used for character array
similar to int *arr;
hence we can’t use char s in strtok because ti require character array not character
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.