Two recent interviews and pen-based questions are interesting:
1. Implement the library function strlen. You cannot use any library function or define any variable.
2. Implement the strrev function. This function has two parameters. Put one character string in reverse order to another character string pointer. Similarly, you cannot use any library function or define any variable.
Recursive Implementation can meet the requirements of these two questions:
- Void strrev (char ** DEST, const char * SRC)
- {
- If (* src = '/0 ')
- Return;
- Else
- {
- Strrev (DEST, SRC + 1 );
- * (* DEST) ++ = * SRC;
- }
- }
- Int strlens (const char * SRC)
- {
- If (* SRC! = '/0 ')
- Return strlens (++ SRC) + 1;
- Else
- Return 0;
- }
The strrev function requires both parameters to be of the char * type, but I did not expect a solution, but Char ** can be used, but this changes the pointer value of the real parameter of the target string, add '/0' at the end '.
Of course, the writing efficiency of these two functions is not necessarily high, but the recursive method is indeed quite interesting.