Replace the comment with one line of C code that will copy the string, source, to the char array, target. int main(void) { char source[] = "COPYME"; char target[100]; char *s = source; char *t = target; /* REPLACE ME! */ printf(target); return 0; }
mind that source is of type char*, while target is of type char[100]
strcpy(target,source); and it's doesn't matter of the type.
oops I forgot to mention no function calls :-P
so in this problem you are basically replacing the strcpy function with one line of basic C code.
you could use a for-loop for the entire operation ..
while (*(target++)) = *(source++)) ;
what's the for loop? klown's way works
hmm actually klown's way doesn't work :( but it does if you replace target and source with t and s
oops, sorry, i got the names mixed up. yes, replace with t and s.
The for-loop works the same way: int main(void) { char* source = "COPYME"; char target[100]; char *s; char *t; for (s = source, t = target; (*t++ = *s++); ); printf(target); return 0; }
why are you not using puts() ?
right puts()!
puts is not the correct answer. That writes a string to standard output, it does not copy a string to memory.
or did you mean replace the printf at the end with puts?
Nah he's just saying why not replace printf with puts if we are allowed variable declarations inside for-loops then we can have: int main(void) { char* source = "COPYME"; char target[100]; for (char *s = source, char *t = target; (*t++ = *s++); ); puts(target); return 0; }
Ktkblown why do you think I don't know that ?
lol
because you said it
the printf() was part of the problem given, in the problem they were not asked to change it
and you misinterpreted it ...
cool we have the strcpy function in one line of code :-D
we should write libraries!
The question was what line do you replace the commented-line with to achieve a goal. Replacing it with 'puts' does the wrong thing. Replacing the printf with puts was not the question and still leave the original question unanswered.
then the problem is bad ..!! evil and agdgdgdgwngo is chanking the problem itself if you can descry that ..
for(int i = 0; i < 6; i++)target[i] = source[i];
that's not C alfie ^^
it is legal C (C99)
yeah sure but your's is not C99 if yes then where are the header-files ?
right mine is not C99 :(
Once I was very stickler in this issues ..but then I fall in love with something else..
Why that's not c? O_O
because it's more common in C++
declaration of variable within the loop for the loop operation.
Well, I study c++ @university so well... but fact a thing "is not common" doesn't mean it's not allowed nor it's not proper of a language, I guess.
http://en.wikipedia.org/wiki/For_loop#C.2B.2B_and_C reads: The ISO/IEC 9899:1999 publication (commonly known as C99) also allows initial declarations in for loops.
C99 is really not the main problem with the for() answer, the main problem is that it breaks on any other string of a different length. You could fix it by replacing the constant 6 with strlen(source), but that breaks the no-function-calls rule in the original problem.
There's no need to shut anyone up here, I am just trying to understand, no need to be mean nor un-polite.
The no function call rule is imposed by OP only .. a for loops is not generally considered as one line solution.
the OP said that was part of the problem, otherwise the answer would just be strcpy
My apologies
my question is evil :-P
But I don't like the way you put the link when I already mentioned that I am aware of C99 specific rules.
I've got another evil question coming up: replace 5 nested for loops with one line of C (including function calls!)
Shoot it but as a new question.
FoolForMath he was posting a reference, which was the right thing to do since there was a disagreement about the rules.
there was no disagreement only a misunderstanding on his behalf, I had already mentioned that things are okay in C99 but not in the archaic and perhaps the most used one
First this question: what is the difference between the output of the nested for-loops and the lone for-loop after the getchar()? int main(void) { int i,j,k,l,m; for (i = 0; i < 10; ++i) for (j = 0; j < 10; ++j) for (k = 0; k < 10; ++k) for (l = 0; l < 10; ++l) for (m = 0; m < 10; ++m) printf("%d%d%d%d%d\n", i, j, k, l, m); getchar(); for (i = 0; i < 100000; ++i) printf("%d\n", i); return 0; }
the first will print a 0-padded 6 digit number and the second will not
great. Now we must figure out how to replace that nested for-loops with one line of C code.
how about printf("%06d", i)
Misunderstanding, disagreement, call it what you want, I just think we are on a friendly website, no need to go all "shut up" . Anyhow, you apologized, I am okay with that, I won't talk anymore about it. :) bye.
FoolForMath, i accept your apology as well.
yeah the printf works! :-D yay
It was not for you ktkown
I accept it anyway
int main(void) { int i; for (i = 0; i < 100000; ++i) printf("%06d\n", i); return 0; }
How could you when it was not for you in the first ? It was only for alfie I haven't said anything wrong to you.
or even: int main(void) { int i; for (i = 0; i < 100000; printf("%06d\n", i++)); return 0; }
what is that agd ?
it does exactly what int main(void) { int i,j,k,l,m; for (i = 0; i < 10; ++i) for (j = 0; j < 10; ++j) for (k = 0; k < 10; ++k) for (l = 0; l < 10; ++l) for (m = 0; m < 10; ++m) printf("%d%d%d%d%d\n", i, j, k, l, m); return 0;} does
so ??
Join our real-time social learning platform and learn together with your friends!