The correct answer to this question is that the question will demonstrate a skill that is bad to have. They want you to demonstrate the ability to write hacker code. They want you to invent your own implementation of the things provided by every C compiler, which is a waste of time. They want you to write streamlined code that by definition is not readable. A 15-line implementation is probably better if it is more readable. Most projects fail, because the developers spent 150 clock cycles. Some of them fail because someone writes unsupported code. If you need to write this, he will need a 15-line comment. So, my answer to this is, show me the performance indicators that protect, so as not to use standard libraries and require the most optimal solution. Time is much better spent designing and collecting these performance indicators.
Never forget - you also interviewed 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 B data // Justification: Performance metric XXX has shown this optimization is needed for(int i=0; szA[i]!='\0'; i++); for(int j=0; (j==0)||(szB[j-1]!='\0'); j++) szA[i+j] = szB[j];
* edit, 9/27/2010
After reading some other solutions, I think that probably the best answer to the code:
//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 do it with a safe version:
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;
And follow this (full answer, which the compiler will optimize the same as above, along with the 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 != '\0') { ++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%s\n\n",szTooShort); my_readable_safe_strcat(szLongEnough,50,szClean); printf("This string should be complete:\n%s\n\n",szLongEnough); my_readable_safe_strcat(szLongEnough,50,szDirty); printf("This string probably has junk data in it, but shouldn't crash the app:\n%s\n\n",szLongEnough); }