zip two strings together

This is a discussion on zip two strings together within the C Programming forums, part of the General Programming Boards category; Code intended to "zip" two strings together. (Some input cases are not defined in this code.) What are bugs in ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    25

    zip two strings together

    Code intended to "zip" two strings together.
    (Some input cases are not defined in this code.)

    What are bugs in the code?. How to fix bugs?.

    Code:
    char *zip(char *a, char *b) {
    char *result;
    int len, i;
    len = strlen(a);
    result = malloc(2*len);
    for(i = 0; i <= len; i++) {
    result[2*i] = a[i];
    result[2*i+1] = b[i];
    }
    return result;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    Right, I should have recognised your behaviour earlier. Read our homework policy. You tell us what are the bugs with the code, and then we'll give you clues on how to fix them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Please help me learn C.
    Last edited by me001; 09-23-2008 at 09:57 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by me001 View Post
    this is not a homework problem..

    i got this from a C interwiew questions docs....

    Please help me learn C.
    And if we give you the correct answers, you will get the job? Or are you just hoping that me telling you that the malloc in the above is wrong, you will automatically be able to recognise such a scenario in another piece of code? Assuming of course that the malloc is actually wrong, which I'm not by any means saying it is, because I think you need to think about what the code does and see if you can figure out what is wrong.

    Without knowing what the preconditions are (such as, do we know anything about the strings a and b that we can use in the code), I can spot at least 3 potential bugs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    From your questions, it is clear that you have not tried compiling C programs and running them recently. As such, I suggest that you go slow and practice with simpler programs first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    i don't need helloworld to learn C. i code enough language....just forgot C...
    Last edited by me001; 09-23-2008 at 09:57 AM.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by me001 View Post
    this is not a homework problem..

    i got this from a C interwiew questions docs....

    Please help me learn C.
    Even if we gave you the answers to these questions you've been posting and explained them to you, you would still not be able to fool an interviewer. Trust me, I am one.
    Besides, what good is it for you to swindle your way into a job, that obviously requires you to know C, when you don't? Don't you think it'll become apparent very quickly, when you fail to write the simplest programs?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    i am going to interview somebody based on these questions...
    Last edited by me001; 09-23-2008 at 09:57 AM.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by me001 View Post
    i am going to interview somebody based on these questions...
    No offence, but are you really the right person to interview someone for a job on a language that you have no idea about???

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    these are random selected code for interview...we need to get answer and discuss...
    Last edited by me001; 09-23-2008 at 09:57 AM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by me001 View Post

    Help and sharing knowledge is good thing...u r loosing nothing by doing that.

    Be postive and help other....
    Fortunately and unfortunately for you, we are helping you and sharing our knowledge. Eventually you will realize that the point of the question you posted (in an interview setting) is to see how well you understand code and spot common trouble areas. Asking us just sees how well we understand code and spot common trouble areas, which is not quite the same thing.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    ok....

    bug 1: both a and b need to be initialized as arrays rather than char
    bug 2: len=strlen(a) will not work, since there is no string length for a initially
    bug 3: one cannot assign malloc(2*len) to a char variable result.

    is this right? can you pls correct me?

    Quote Originally Posted by matsp View Post
    And if we give you the correct answers, you will get the job? Or are you just hoping that me telling you that the malloc in the above is wrong, you will automatically be able to recognise such a scenario in another piece of code? Assuming of course that the malloc is actually wrong, which I'm not by any means saying it is, because I think you need to think about what the code does and see if you can figure out what is wrong.

    Without knowing what the preconditions are (such as, do we know anything about the strings a and b that we can use in the code), I can spot at least 3 potential bugs.

    --
    Mats

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by me001 View Post
    ok....

    bug 1: both a and b need to be initialized as arrays rather than char
    bug 2: len=strlen(a) will not work, since there is no string length for a initially
    bug 3: one cannot assign malloc(2*len) to a char variable result.

    is this right? can you pls correct me?
    1. a and b are arrays
    2. len=strlen(a) is probably not the right line of code for this situation, but not for the reaon you state, because the reason you state makes no sense
    3. No one is trying to assign malloc(2*len) to a char variable (i.e., result is not a char variable).

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm with QuantumPete on this: If you are going to ask technical programming questions, I'd say you need to have someone who understands the answers. Because if I answer your question, then you would not necessarily get the same answer from someone else, and someone third may spot something I hadn't thought of.

    My first question if I was given that code would be "Are a and b strings always guaranteed to be the same length". If that's SUPPOSED to be the case, I'd still check that strlen(b) == len, and return some sort of error (NULL?) if it is not.

    The code is broken in that it copies past the end of result's memory allocation, for sure. The allocation needs to be 2*(len+1).

    You may want to check for NULL inputs, zero length inputs, and check if the memory allocation was successful or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    bug 1: both a and b need to be initialized as arrays rather than char
    It is true that a and b should point to characters that are part of an array of characters, but making a and b char* parameters is not a bug since arrays are converted to a pointer to their first element when passed as an argument to a function.

    bug 2: len=strlen(a) will not work, since there is no string length for a initially
    Again, this is not true, since strlen() operates on a string, that is true, but what is passed to it is a pointer to a character (usually the first) in the string.

    bug 3: one cannot assign malloc(2*len) to a char variable result.
    That is true, but result is a char*, not a char.

    EDIT:
    Quote Originally Posted by matsp
    The code is broken in that it copies past the end of result's memory allocation, for sure. The allocation needs to be 2*(len+1).
    I think that 2*len+1 will suffice, assuming that both a and b are of length len.
    Last edited by laserlight; 09-23-2008 at 09:57 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21