What are the differences between strcpy and memcpy? The strcpy () function can only copy strings. The strcpy () function copies each byte of the source string to the destination string. When a NULL character ('\ 0') at the end of a string is encountered, it ends the copy and automatically adds' \ 0' to the end of the target string'
The memcpy () function can copy any type of data. Because not all data ends with NULL characters, you must specify the number of bytes to copy for the memcpy () function. When copying a string, the strcpy () function is usually used. When copying other data (such as a structure), the memcpy () function is usually used.
Void * memcpy (char * DEST, const char * SRC, size_t size)
{
Assert (DEST! = NULL) & (SRC! = NULL ));
Char * P = DEST;
Char * q = SRC;
While (size --> 0)
* P ++ = * q ++;
Return DEST;
}// Shift n-bit function to the right of the string // write a function to shift n loops of a char string to the right. For example, the original function is "abcdefghi"
// If n = 2, it should be "hiabcdefg"
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Define MAX_LEN 20/* void LoopMove (char * src, int n)
{
Int m = strlen (src)-n;
Char temp [MAX_LEN];
Strcpy (temp, src + m) // copy the last n characters first and copy them first.
Strcpy (temp + n, src); // copy the entire string
// * (Temp + strlen (src) = '\ 0'; // Add the ending character
Strcpy (src, temp );
} */Void LoopMove (char * src, int n)
{
Int M = strlen (SRC)-N;
Char temp [max_len];
Memcpy (temp, SRC + m, n); // copy the last n characters of the string to the temp array.
Memcpy (SRC + N, SRC, m); // leave the n characters Blank Based on the source string, then copy the first M characters to a proper position. // SRC has an ending character, so you do not need to add it.
Memcpy (SRC, temp, n); // copy the last n characters to a proper position.
}
Int main ()
{
Char STR [] = "abcdefghi ";
Loopmove (STR, 2 );
Printf ("% s \ n", STR );
Return 0 ;}