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; }
6 Respuestas
+ 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! 🖐️😎
+ 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.
+ 1
Possibly you overlooked where it mentioned there are two possible formats.
+ 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;
}
+ 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]);
0
Brian
printf("%s", &input[++x]); works
Allthow I dont know why the ‚&‘ and the ‚++‘ before the int x