Ask your own question, for FREE!
Mathematics 7 Online
OpenStudy (anonymous):

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; }

OpenStudy (anonymous):

mind that source is of type char*, while target is of type char[100]

OpenStudy (anonymous):

strcpy(target,source); and it's doesn't matter of the type.

OpenStudy (anonymous):

oops I forgot to mention no function calls :-P

OpenStudy (anonymous):

so in this problem you are basically replacing the strcpy function with one line of basic C code.

OpenStudy (anonymous):

you could use a for-loop for the entire operation ..

OpenStudy (anonymous):

while (*(target++)) = *(source++)) ;

OpenStudy (anonymous):

what's the for loop? klown's way works

OpenStudy (anonymous):

hmm actually klown's way doesn't work :( but it does if you replace target and source with t and s

OpenStudy (anonymous):

oops, sorry, i got the names mixed up. yes, replace with t and s.

OpenStudy (anonymous):

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; }

OpenStudy (anonymous):

why are you not using puts() ?

OpenStudy (anonymous):

right puts()!

OpenStudy (anonymous):

puts is not the correct answer. That writes a string to standard output, it does not copy a string to memory.

OpenStudy (anonymous):

or did you mean replace the printf at the end with puts?

OpenStudy (anonymous):

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; }

OpenStudy (anonymous):

Ktkblown why do you think I don't know that ?

OpenStudy (anonymous):

lol

OpenStudy (anonymous):

because you said it

OpenStudy (anonymous):

the printf() was part of the problem given, in the problem they were not asked to change it

OpenStudy (anonymous):

and you misinterpreted it ...

OpenStudy (anonymous):

cool we have the strcpy function in one line of code :-D

OpenStudy (anonymous):

we should write libraries!

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

then the problem is bad ..!! evil and agdgdgdgwngo is chanking the problem itself if you can descry that ..

OpenStudy (alfie):

for(int i = 0; i < 6; i++)target[i] = source[i];

OpenStudy (anonymous):

that's not C alfie ^^

OpenStudy (anonymous):

it is legal C (C99)

OpenStudy (anonymous):

yeah sure but your's is not C99 if yes then where are the header-files ?

OpenStudy (anonymous):

right mine is not C99 :(

OpenStudy (anonymous):

Once I was very stickler in this issues ..but then I fall in love with something else..

OpenStudy (alfie):

Why that's not c? O_O

OpenStudy (anonymous):

because it's more common in C++

OpenStudy (anonymous):

declaration of variable within the loop for the loop operation.

OpenStudy (alfie):

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.

OpenStudy (alfie):

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.

OpenStudy (anonymous):

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.

OpenStudy (alfie):

There's no need to shut anyone up here, I am just trying to understand, no need to be mean nor un-polite.

OpenStudy (anonymous):

The no function call rule is imposed by OP only .. a for loops is not generally considered as one line solution.

OpenStudy (anonymous):

the OP said that was part of the problem, otherwise the answer would just be strcpy

OpenStudy (anonymous):

My apologies

OpenStudy (anonymous):

my question is evil :-P

OpenStudy (anonymous):

But I don't like the way you put the link when I already mentioned that I am aware of C99 specific rules.

OpenStudy (anonymous):

I've got another evil question coming up: replace 5 nested for loops with one line of C (including function calls!)

OpenStudy (anonymous):

Shoot it but as a new question.

OpenStudy (anonymous):

FoolForMath he was posting a reference, which was the right thing to do since there was a disagreement about the rules.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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; }

OpenStudy (anonymous):

the first will print a 0-padded 6 digit number and the second will not

OpenStudy (anonymous):

great. Now we must figure out how to replace that nested for-loops with one line of C code.

OpenStudy (anonymous):

how about printf("%06d", i)

OpenStudy (alfie):

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.

OpenStudy (anonymous):

FoolForMath, i accept your apology as well.

OpenStudy (anonymous):

yeah the printf works! :-D yay

OpenStudy (anonymous):

It was not for you ktkown

OpenStudy (anonymous):

I accept it anyway

OpenStudy (anonymous):

int main(void) { int i; for (i = 0; i < 100000; ++i) printf("%06d\n", i); return 0; }

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

or even: int main(void) { int i; for (i = 0; i < 100000; printf("%06d\n", i++)); return 0; }

OpenStudy (anonymous):

what is that agd ?

OpenStudy (anonymous):

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

OpenStudy (anonymous):

so ??

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!