I was asked this question in the MS written walkin-interview:
Find errors in the program below which is supposed to to return a new string with \n
appended to it.
char* AddnewlinetoString(char *s)
{
char buffer[1024];
strcpy(buffer,s);
buffer[strlen(s)-1] = '\n';
return buffer;
}
Thank you all.
I'd tried to code on myself and was able to get it working by making buffer variable global and having buffer[strlen(s)] = '\n'
. But did not know there were many other bugs in it.
Appreciate your time.