The Shortest [Problem of the week]

Since i am extremely bored, and i have been staring into lots of code recently .. i have  no intention to do this in the blog .. so this weeks problem will be about the shortest code ever ..

ok .. go on .. i need the Shortest implementation of..

strcpy()

 

yeah.. strcpy().. our strcpy().. 😉

void strcpy(char *destination, const char *source);

Rules:

  1. any language is welcome .. but C/C++ are preferred.
  2. any library functions or classes are absolutely not welcome..
  3. i don’t care for the return .. keep it void
  4. the function must take 2 arguments, one is the destination string and the other is the source string .. you can assume that both are valid (not null) pointers nor empty strings.. so forget your QA sprit for a while
  5. all strings are “null terminated char arrays”.. very normal “C strings”.
  6. input string must be kept intact, you can notice const char *
  7. read the first comment about the const char * 😀
  8. send me CODE .. and only CODE .. you can explain it , you can comment it .. but don’t send ideas alone 😀 .. try to code it 😀 .. this is the challenge.
  9. for the new faces .. welcome! 😀 .. you post the solution in the comments, and if it is right i will not approve it until it’s time, otherwise, i will approve it and say what went wrong.. that’s why you should read the comments before posting a solution.

after we are done ان شاء الله.. i will show you how to make use of a couple of features in C++ to solve this problem .. one of them is EXTREAMELY annoying feature..

OK .. That’s it .. let the race .. begin .. 😀

the solution can be found here

Advertisements

55 thoughts on “The Shortest [Problem of the week]

  1. ok .. a trick i read in “Object Oriented Programming in C++ by Robert Lafore” i think about how to differentiate between “const int*” and “int* const x”
    —–
    he said “read it from right to left” so

    const int* x;

    is read: x is a pointer (*) to a constant integer (const int)
    means that you can say x = NULL but u can’t say *x = 5
    —–
    and

    int* const x;

    is read: x is a constant (const) pointer(*) to an integer int
    means that you cannot say x = NULL but you can say *x = 5

  2. I’ll suggest two solutions:

    while(*source != '')
    *destination++ = *source++;
    *destination = '';

    .. and ..

    do {
    *destination++ = *source++;
    } while(*(source-1) != '');

  3. I’ll suggest two solutions:

    while(*source != '')
    *destination++ = *source++;
    *destination = '';

    .. and ..

    do {
    *destination++ = *source++;
    } while(*(source-1) != '');

  4. again ..another winner .. now i will sort solutions by places 😀

    Fist Place 2 Lines
    Alaa Shaker

    Second Place 3 Lines
    Alaa Shaker

    again .. loool
    now u have to beat Alaa’s code .. and give me 2 lines with less characters .. or cover more test cases ..

  5. void strcpy(char *destination, const char *source);
    {

    //Note this code assumes that the source string is null terminated

    while( *destination ++ = *source++ )
    // or more clearer one –> while((*destination ++ = *source++) != 0)

    the code will loop untill hit the null character

    }

  6. #include “stdafx.h”
    #include “iostream”
    using namespace std;

    void StrCopy( char* destination, const char *source)
    {
    do{ *(destination++) = *(source++);}
    while( *(destination-1) != ” );
    }

    int main()
    {
    const char* source = “Mohammad Fouad” ;
    char* buffer = new char[100];

    StrCopy( buffer, source);

    cout<<buffer<<endl;

    return 0;
    }

  7. void StrCopy( char* destination, const char *source)
    {
    while( *(destination) != ”)
    *(destination++) = *(source++) ;
    }

  8. Great! .. we have a tie 😀

    Fist Place 1 Line!

    AlaaShaker , Mohammad Diab (tie)

    Second Place 2 Lines

    Mahmoud Osama


    thanks guys
    any one else 😀 ?

  9. WOW .. this is going too fast 😀 !!! .. it has been only 3 hours and we have 3 ties

    Fist Place 1 Line!

    AlaaShaker , Mohammad Diab, Mahmoud Osama (tie)

    any one else 😀 ?

  10. void _strcpy(char *destination, const char *source)
    {
    for(int i=0;source[i-1]!=”||(i-1)==-1;i++)destination[i] = source[i];
    }

    but a Question …. must i create a copy or i can make both point to the same place 😀
    as i think i can use cont_cast 😀

  11. i think this is better 😀
    void _strcpy(char *destination, const char *source)
    {
    for(int i=0;source[i-1]!=”||(i)==0;destination[i] = source[i],i++);
    }

  12. another tie 😀

    Fist Place 1 Line!

    AlaaShaker , Mohammad Diab, Mahmoud Osama, Anonymous! (tie)

    Second Place 2 Lines

    Tecnoyotta


    To Tecno

    but a Question …. must i create a copy or i can make both point to the same place 😀
    as i think i can use cont_cast 😀

    off course yon CANNOT do that, Strcpy’s behavior is well knowNNN, besides, you promised the user of your function “Implicitly” that your function will not mess with the contents of the Source string const char* so u cannot let another pointer to point to it.. that will endanger the data of the source if the user messes with destination

    any one else 😀 ?

  13. void _strcpy(char *destination, const char *source)
    {
    while(*destination++ = *source++);
    }

    looool this is allowed in MinGW compiler but fouad helped me and remembered me that \ 0 is false 😀

  14. First year students are here 😀

    First Place 1 Line!

    AlaaShaker , Mohammad Diab, Mahmoud Osama, Anonymous, Tecnoyotta, Amr Saqr (tie)

    form 3 lines to 1 !!! .. seems like C++ can really get verbose sometimes.. 😀

  15. Does this count for a single line?

    void strcpy(char *destination, const char *source)
    {
    for(int i=0;destination[i] = source[i];i++);
    }

  16. To Hatem:
    yes .. that’s it (Y) .. thanks

    First Place 1 Line!

    AlaaShaker , Mohammad Diab, Mahmoud Osama, Anonymous, Tecnoyotta, Amr Saqr, Hatem (tie)

  17. I’m not into C++, but will give it a shot anyway!

    void strcpy(char *destination, const char *source)
    {
    for(int i = 0; ( (destination[i] = source[i]) || 1 ) && source[i] != NULL ; i++);
    }

  18. It’s kind of an old problem, from “The C Programming Language”, the book that *introduced* C!
    It assumes an old-style architecture where a char is always 1 byte.

    void strcpy(char *d, char *s)
    {
    while(*(d++) = *(s++));
    }

  19. another two winners 😀
    Mohamed Samy and Metal

    To Mohamed Samy
    yes .. very elegant, the whole point of the problem .. i didn’t read that book, but i will mention the source isA when i stop getting solutions

    To Metal
    if you’r not into C++ and u got it like this .. then i am a genius in C++ 😀 .. which is not the case .. sadly .. 😀
    so .. can you make it a bit shorter 😀 ?

  20. void strcpy(char *destination, const char *source)
    {
    while( *source ){*destination++ = *source++;}
    *destination=”;
    }

  21. First Place 1 Line!

    AlaaShaker , Mohammad Diab, Mahmoud Osama, Anonymous, Tecnoyotta, Amr Saqr, Hatem, Metal (tie)

    Second Place 2 Lines

    Mohamed Hesham

    thanks every one 😀 .. great job !

  22. void scpy(char *destination, const char *source)
    {
    do{*destination++ = *source++;}while( *(destination-1) );
    }

  23. To Mohamed Hesham
    still these are 2 lines .. your first solution was actually much nearer to the shortest version ..

  24. sry fouad but if ur comparing winners for 1line with character it is my solution rather than the prev

    void scpy(char *destination, const char *source)
    {
    while( *destination++ = *source++ );
    }

    sorry for that code mess

  25. To Mohamed Hesham
    Perfect! .. you got it man (Y) .. the shortest 1 line solution

    First Place 1 Line!

    AlaaShaker , Mohammad Diab, Mahmoud Osama, Anonymous, Tecnoyotta, Amr Saqr, Hatem, Metal, Mohammed Hesham (tie)

  26. i think this problem is over with Flpino’s Solution :p .. gj dude .. i couldnt think of it due to some problems

    fouad .. mind if i ask u when will u reveal the stuff ?? iam curious ..

  27. To Memo
    actually 97% of us didn’t get it from the first trial ..

    i will reveal it either tomorrow night or Sunday’s night isA
    i think i am waiting for the tenth winner 😀

  28. To Metal
    loooooooooooooool .. i knew you will get it
    thanks for trying 😀 (Y) (Y)

    waiting for a 10th winner 😀 .. any one ?

  29. Actually man we are 10 lol
    AlaaShaker , Mohammad Diab, Mahmoud Osama, Anonymous, Tecnoyotta, Amr Saqr, Hatem, Metal, Mohammed Hesham (tie)
    You missed Dr. Mohammed Samy 😉

  30. Ahlan! .. i will blame it on Copy&Paste 😀 .. or may be on Noor Series cuz its running now and i get distracted 😀 .. lool

    yes winners are 10 including Mohamed Samy .. sorry for being “mesh merakkez” ! :$

    and we got an eleventh winner Mohamed Abdelghani
    he sent 1 line .. can you make it shorter 😀 ? .. but actually you used the feature i am looking for 😀

  31. Dude … sanawat el daya3 ashad :D:D
    you said :
    after we are done ان شاء الله.. i will show you how to make use of a couple of features in C++ to solve this problem .. one of them is EXTREAMELY annoying feature..

    I hope u will cover callbacks and functors 😀 need some real tasks on em , they r the most … No comment .. 😀 anway any application on function pointers should be in ur plans for the comming problems .. ty fouad

  32. # include
    using namespace std;
    void Strcpy(char *destination,const char *source)
    {
    /*
    while source not reached to NULL character
    copy its content to destination
    */
    while(*destination++ = *source++){}
    }
    int main()
    {
    char *source,destination[100]; // define source & destination
    source = “Mohamed”; // intialization
    Strcpy(destination,source); // call Strcpy fun
    cout << destination << endl; // print copied string
    }

  33. You know, I read the problem & went to sleep.. while I was in bed I remembered I didn’t solve it & a solution pumped to my brain.. so I woke up, wrote that code & comment 😀

    while(*destination++ = *source++){}

    Good Night 😀 ..

  34. Another 3 winners :D,
    -Roaa and Mohamed Abd-Elmone’m 1line
    -Tasniem 2 lines

    To Roaa:
    next time i will disable comments after 8 pm 😛 ..

    To Mohamed Abd-Elmone’m
    great job man .. (Y) .. waiting for your solution for in the upcoming problems isA

    To Tasniem:
    waiting for the 1 line solution .. 😀

    now we have 14 winners 😀 .. this is far beyond what i expected .. !

  35. Pingback: The Shortest - Solved [Problem of the Week ] « more about the 8

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s