0

YouTube Link Finder

Can you find the mistake? This is reading a YouTube-link and prints out just the last part of the link. #include <stdio.h> #include <string.h> int main() { int x=0, i=0; char input[50]; gets(input); while(input[x] != '=') { x++;} while (x < strlen(input)) { char output[20]; strcpy(output[i],input[x]); i++; x++; } printf("%s", output); return 0; }

26th Nov 2022, 9:54 PM
Gregory Hller
6 Antworten
+ 3
Here you have as many as 6 errors, plus two unnecessary operations and one extra variable, but even if you fix all the errors, the program will print the first part of the address, not the last. The strcpy() function is not needed here, it copies strings, as its name says: "string copy", you are trying to use two character values in it, although this is enough: output[i] = input[x]; But two different indices are also not needed here. I hope you will find the rest of the visual errors yourself. When you correct this code, then we will talk about how to print the last part of the name. Happy coding! 🖐️😎
27th Nov 2022, 2:11 AM
Solo
Solo - avatar
+ 2
The & is the syntax for getting the pointer to a memory location. You could as well use printf("%s", input + ++x). The ++ was necessary because after the loop, x is the index of '='. It needs to be incremented to the next character.
27th Nov 2022, 1:41 PM
Brian
Brian - avatar
+ 1
Possibly you overlooked where it mentioned there are two possible formats.
26th Nov 2022, 10:08 PM
Brian
Brian - avatar
+ 1
Brian Solo See whats missing? #include <stdio.h> #include <string.h> int main() { int x=0; char input[50]; gets(input); if(strlen(input)> 30) { while(input[x] != '=') { x++; } while (x < strlen(input)) { x++; printf("%c", input[x]); } } else if (strlen(input)< 30) { x=17; while (x<=strlen(input)) { printf("%c", input[x]); x++; } } return 0; }
27th Nov 2022, 9:16 AM
Gregory Hller
+ 1
Gregory Hller the code is nearly done. The only problem is that the printing loops run past the end of the string. Strlen() is too many characters when you start at position x. Instead of printing one character at a time, it is easier to print it as a string. Supply the pointer to the first character and let printf find the terminating null. For example: printf("%s", &input[++x]);
27th Nov 2022, 12:34 PM
Brian
Brian - avatar
0
Brian printf("%s", &input[++x]); works Allthow I dont know why the ‚&‘ and the ‚++‘ before the int x
27th Nov 2022, 1:20 PM
Gregory Hller