This question is basically similar to my previous interview with Baidu. In addition to examining the interviewer's data structure, encoding capabilities, programming style, and so on, I particularly examine the robustness of the code prepared by the interviewer, that is, all exceptions in the program must be taken into account. Today, I just saw this example, which is very representative and inspiring. I sorted out my ideas and shared them.
1. Subject: Write a function to copy strings. Given the prototype of strcpy:
Char * strcpy (char * dest, const char * src );
Requirements:
(1) do not call any library functions.
(2) explain why the function returns char *.
2. Solution:
Char * strcpy (char * dest, char * src)
{
If (dest = NULL) | (src = NULL ))
{
Printf ("arg wrong ");
Return NULL;
}
Char * ret_string = dest;
While (* dest ++ = * src ++ )! = '\ 0 ');
Return ret_string;
}
3. common error parsing:
(1) The function should first judge whether the pointer dest and src are NULL without checking the pointer validity. If the code for checking input parameters is missing, it means that the respondent does not pay attention to the robustness of the Code.
(2) Use if ((! Dest) | (! Src) if (dest = 0) | (src = 0 )). It indicates that the respondent's writing code is not standard. Dest and src are pointer variables, and! It targets logical variables. Using 0 instead of NULL reduces the maintainability of the program, which means that the respondent does not know the advantages of using a NULL constant. When determining whether the pointer is a NULL pointer, we recommend that you use NULL instead of 0.
(3) the dest pointer is not returned, indicating that the respondent does not know why the return type of the function is char *. The reason for returning the dest pointer is to implement a chained expression, such:
Int length = strlen (strcpy (dest, "hello world "));
Strlen returns the length of the string pointed to by the copy dest. If the pointer dest is not returned, you need to write two statements, which can be completed only in one statement.
(4) The string copy operation is. If you forget to copy the last '\ 0 '. The preceding while statement is the most simplified method.
Of course, to improve the readability of the program, it is best to make it as easy as possible for others to understand. For example:
While (* src! = '\ 0 ')
{
* Dest = * src;
Dest ++;
Src ++;
}
* Dest = '\ 0 ';
4. Complete implementation code:
# Include <stdio. h>
# Include <stdlib. h>
Char * strcpy (char * dest, char * src)
{
If (dest = NULL) | (src = NULL ))
{
Printf ("arg wrong ");
Return NULL;
}
Char * ret_string = dest;
While (* src! = '\ 0 ')
{
* Dest = * src;
Dest ++;
Src ++;
}
* Dest = '\ 0'; // If you comment out this line, no error is reported, but pay attention to the robustness of the program.
Return ret_string;
}
Int main ()
{
Char str [] = "hello ";
Int n = sizeof (str); // includes the last character '\ 0'
Char * p = (char *) malloc (n );
Strcpy (p, str );
Printf ("p: % s \ n", p );
Return 0;
}
Output:
P: hello