Today, I saw a C-language string copy interview question on csdn. The question is as follows:
The input string must be output in reverse order, and third-party variables cannot be used. The prototype of the function is char * strncopy (const char * Source, char * DEST), and database functions are not required,
As required, I wrote the following program:
# Include <stdio. h> # include <stdlib. h> char * strncopy (const char * Source, char * DEST); int main (INT argc, char * argv []) {char * Source = "1241654654 "; char Dest [strlen (source)]; printf ("% d \ n", strlen (DEST); strncopy (source, DEST); printf ("% s \ n ", DEST); System ("pause"); Return 0;}/***** reverse copy */char * strncopy (const char * Source, char * DEST) {If (source! = NULL & DEST! = NULL) {While (* ++ source); While (* DEST ++ = * -- source); Return DEST;} return NULL ;}
The above Code seems to have implemented the function, but there is still a fatal error, that is, it does not determine the length of the dest, and does not consider whether the target string can accommodate the next source.