0
I get segmentation fault for my code in Cxxdroid but somehow passed all the test cases in sololearn. How's that possible? Thanks
My code below is the code for YouTube link finder quiz https://code.sololearn.com/c7FbCp0oixSe/?ref=app
8 odpowiedzi
+ 1
Rishi,
Notice that the first token was stored in <charptr1>
charptr1 = strtok(link,"=/");
So the next tokens should also be stored in <charptr1>. But look at the while-loop condition, <charptr2> is used there
while(charptr2!=NULL)
And also look at the loop body
{
charptr1=charptr2;
charptr2=strtok(NULL,"=/");
}
You assigned <charptr2> to <charptr1>. And then you try to get and store next token in <charptr2>, where you should be using <charptr1>.
+ 1
"How's that possible?"
I don't know how or why SoloLearn let the code to pass the tests.
But your way of using strtok() was incorrect. I'm guessing that is the cause of the segmentation fault. Review the atrtok() reference on how to use it
http://www.cplusplus.com/reference/cstring/strtok/
You can safely find the '=' which locates the video ID in the URL. Having the '=' position at hand, you can figure out how to print the substring after the '='
http://www.cplusplus.com/reference/cstring/strchr/
+ 1
Ipang can you please explain me? Cause I can't get how I used strtok() wrong. Also the website mentions the same way how I used it
+ 1
Ipang yes I thought the function strtok can be used with any identifier as long as it has the same argument to split. And isn't charptr1=charptr2 just assign the address of the next token to charptr1? Sorry if my logic is wrong
+ 1
Rishi,
No, to get the next tokens, strtok() should work with the same pointer, in this case <charptr1>. I don't think you need <charptr2> at all.
I think you can use strstr() to find the video ID in the URL rather than strtok(), for this case.
http://www.cplusplus.com/reference/cstring/strstr/
+ 1
Rishi,
Here's a sample of using strstr() to extract video ID
https://code.sololearn.com/cEa6Gb4EBxiW/?ref=app
+ 1
Ipang thank you so much. I got it now
+ 1
Okay 👍