0
somebody help me for creating youtube link
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 How to create yt link on c++ and python? many times i learned but still can't find solution for that code, please help me..
4 Answers
+ 1
Hello febrio pratama
In the first link you need to find the index of "=".
Lets say length is 10 and the equal sign is at index 5 -> sunstring from 6 to 10.
In the second link you could start from the end of the link. Search until you find the first /
Take the index + 1, substring from there to the end. Done.
+ 1
Look for either 'watch?v=' or 'youtu.be/'. If either one of those present in given URL, then extract substring from the given URL after either the 'watch?v=' or 'youtu.be/' part.
Hint: Use slicing method to grab the substring.
0
My code:
url = input()
print(url[-11:])
It's almost certainly going to stay at 11 characters. The individual characters come from a set of 64 possibilities (A-Za-z0-9_-).
Source: https://stackoverflow.com/questions/6180138/whats-the-maximum-length-of-a-youtube-video-id/42119442#42119442
and my other version would be as below code
url = input()
if "youtube.com" in url:
print(url[url.index("=")+1:])
else:
for i in range(len(url)):
if url[-i-1] == "/":
print(url[-i:])
break
in question its mentioned that urls can be of only two types ... so my both codes are perfect. but in real world, YouTube has many different types of url formats..... you can check at my sources link.
- 1
x=input()
for j in range(len(x)-1,0,-1):
if x[j]=="/" or x[j]=="=":
print(x[j+1:])
break