Interview Question-Concatenate two Strings without using strcat in C

Interview Question-Concatenate two Strings without using strcat in C

I expect they wanted something like this:

void mystrcat(char * dest, const char * src)
{
    //advance dest until we find the terminating null
    while (*dest) ++dest;

    //copy src to dest including terminating null, until we hit the end of src
    //Edit: originally this: 
    //for (; *dest = *src, *src; ++dest, ++src);
    //...which is the same as this
    for (; *dest = *src; ++dest, ++src);
}

It doesnt return the end of the concatenated string like the real strcat, but that doesnt seem to be required.

I dont necessarily know if this sort of thing is a good interview question – it shows that you can code tersely, and that you know what strcat does, but thats about it.

Edit: as aib writes, the statement

while (*dest++ = *src++);

…is perhaps a more conventional way of writing the second loop (instead of using for).

Given that the task was to concatenate two strings, not to create a duplicate of strcat, Id go with the simple option of creating a completely new string that is a combination of the two.

char buffer[REASONABLE_MAX] = {0};
snprintf(buffer, REASONABLE_MAX - 1, %s%s, string1, string2);

Interview Question-Concatenate two Strings without using strcat in C

The proper answer to that question is that the question would demonstrate a skill that it is bad to have. They are wanting you to demonstrate the ability to write hacker code. They are wanting you to invent your own implementation of things provided already by every C compiler, which is waste of time. They are wanting you to write streamlined code which, by definition, is not readable. The 15 line implementation is probably better if it is more readable. Most projects do not fail because the developers wasted 150 clock cycles. Some do fail because someone wrote unmaintainable code. If you did have to write that, it would need a 15 line comment. So my answer to that would be, show me the performance metrics that defend needing to not use the standard libraries and requiring the most optimal solution. Time is much better spent on design and gathering those performance metrics.

Never forget – you are also interviewing them.

 //assuming szA contains first string and szB contains second string
 //and both are null terminated
 //  iterate over A until you get to null, then iterate over B and add to the end of A
 //  and then add null termination to A
 //  WARNING:  memory corruption likely if either string is not NULL terminated
 //  WARNING:  memory corruption likely if the storage buffer for A was not allocated large
 //            enough for A to store all of Bs data
 //  Justification:  Performance metric XXX has shown this optimization is needed
 for(int i=0; szA[i]!=; i++); 
 for(int j=0; (j==0)||(szB[j-1]!=); j++) szA[i+j] = szB[j];

*edit, 9/27/2010

After reading some other solutions to this, I think the following is probably the best code answer:

 //Posted by Doug in answer below this one
 void my_strcat(char * dest, const char * src)
 {    
      while (*dest) ++dest;    
      while (*dest++ = *src++);     
 }

But I would follow that up with a safe version of that:

 void my_safe_strcat(char * dest, const unsigned int max_size, const char * src)
 {
      int characters_used=0;
      while (*dest) { ++dest; characters_used++; }
      while ( (characters_used < (max_size-1) ) && (*dest++ = *src++) ) characters_used++;
      *dest = 0; //ensure we end with a null
 }

And follow that up with (full answer, which compiler will optimize to be the same as above, along with application which was the real question):

void my_readable_safe_strcat(char * dest, const unsigned int max_size, const char * src)
{
    unsigned int characters_used = 0;
    while (*dest != ) 
    { 
        ++dest; 
        characters_used++;   
    }
    while ( (characters_used < (max_size-1) ) && (*dest = *src) ) 
    {
        dest++;
        src++;
        characters_used++;
    }
    *dest = 0; //ensure we end with a null
}



int _tmain(int argc, _TCHAR* argv[])
{
    char szTooShort[15] = First String;
    char szLongEnough[50] = First String;
    char szClean[] = Second String;
    char szDirty[5] = {f,g,h,i,j};

    my_readable_safe_strcat(szTooShort,15,szClean);
    printf(This string should be cut off:n%snn,szTooShort);

    my_readable_safe_strcat(szLongEnough,50,szClean);
    printf(This string should be complete:n%snn,szLongEnough);

    my_readable_safe_strcat(szLongEnough,50,szDirty);
    printf(This string probably has junk data in it, but shouldnt crash the app:n%snn,szLongEnough);

}

Leave a Reply

Your email address will not be published. Required fields are marked *