+ 1

Why is this program a mistake? [5]

Could anyone explain it to me why the first code (the bugged one) gives error in codecoach challenge output, although the result is same as expected output (from 1st and 2nd test case output)? https://sololearn.com/compiler-playground/cZMbZukJGQx3/?ref=app https://sololearn.com/compiler-playground/c5IsyCj1SaWf/?ref=app

26th Sep 2024, 3:49 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
12 ответов
+ 4
Celvien Kurniawan the first one is picking up the "\0" as a character though it counts the actual characters ( strlen ) in the tail as 11 so it adding "\0" adds 12 or a whitespace the do - while should have been a while loop which will now eliminate the extra whitespace. p++; while (*p != '\0') { printf("%c", *p); p++; }
26th Sep 2024, 5:43 PM
BroFar
BroFar - avatar
+ 4
Celvien Kurniawan the first code works while the second is looking for a number "%s" *p remove the * just p
26th Sep 2024, 4:58 PM
BroFar
BroFar - avatar
+ 3
Celvien Kurniawan remember you actually have to work with both types of YouTube links the long version ie ... https://www.youtube.com/watch?v=RRW2aUSw5vU and the shorter version ie ... youtu.be/RRW2aUSw5vU Here is the complete Task: Create a program that parses through a link, extracts and outputs the YouTube video ID. Input Format: A string containing the URL to a YouTube video. The format of the string can be in "https://www.youtube.com/watch?v=kbxkq_w51PM" or the shortened "https://youtu.be/KMBBjzp5hdc" format. Output Format: A string containing the extracted YouTube video id. Sample Input: https://www.youtube.com/watch?v=RRW2aUSw5vU Sample Output: RRW2aUSw5vU
26th Sep 2024, 4:36 PM
BroFar
BroFar - avatar
+ 2
Celvien Kurniawan change char s[250], to char s[500], you also didn't allow enough here the string is [493]
26th Sep 2024, 4:53 PM
BroFar
BroFar - avatar
+ 2
Amanuel BeAman welcome to Sololearn but your comment is offtopic for this thread please search the question and answer forum. https://www.sololearn.com/discuss/1316935/?ref=app https://www.sololearn.com/discuss/3021159/?ref=app
27th Sep 2024, 3:13 PM
BroFar
BroFar - avatar
+ 2
Celvien Kurniawan Do you really need a while loop to display the string? #include <stdio.h> #include <string.h> int main() { char link[500]; scanf("%s", link); char* vidId = strrchr(link,'='); puts((vidId) ? ++vidId : (strrchr(link,'/')+1)); }
28th Sep 2024, 11:47 AM
Bob_Li
Bob_Li - avatar
+ 1
BroFar eh? But the second code (youtube link finder(true?))is considered correct by codecoach test case, tho. Also i have changed the size of s to 500 in the first code, but the error message (cross mark) still persist.
26th Sep 2024, 5:06 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 1
Oh, thanks For pointing it out (*).
26th Sep 2024, 5:27 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 1
I see. Thanks for the info. I finally understand why.
27th Sep 2024, 2:23 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 1
Bob_Li wow. It can be made that way... Awesome.
29th Sep 2024, 1:35 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
BroFar yeah, that's why in the code after i use: p=strrchr(s,'='); //for the long URL (cursor setting) i also use: if(p==NULL) p=strrchr(s,'/'); //for the short URL (cursor setting). Then i use p++; to move the cursor to the right so i won't take '/' or '=' in the output string. Then big question for me is... Why the output of the first code is considered mistake (has error) while it has the exact same output like in the second code?
26th Sep 2024, 4:50 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
Why isn't add image to our web this app for our website is not show images.
27th Sep 2024, 2:26 PM
Amanuel Adane