strlen function, strcat function, strcpy function, strncpy function, strcmp function
strcpy function : [CPP] view plain copy char *strcpy (char *dest, const char *src) {assert (Dest!= L) && (SRC!= NULL)); char *address = Dest; while ((*dest++ = *src++)!= ' ") NULL; return address; }
strncpy function :
Using the standard library function strncpy (), you can copy part of a string into another string . The strncpy () function has 3 parameters: The first argument is a directory string, the second argument is the source string, and the third argument is an integer that represents the number of characters to copy from the source string to the destination string. [CPP] view plain copy char *strncpy (char *strdest, const char *STRSRC, int n) {assert (strdest!= NULL ) && (strsrc!= NULL)); char *address = strdest; while (n--> 0) *strdest++ = *strsrc++; return address; }
strcmp function :
The strcmp function is the basic function in C/s + +, which compares two strings and returns the results of the comparison, as follows:
int strcmp (CONSTCHAR*STR1,CONSTCHAR*STR2);
Where str1 and str2 can be string constants or string variables, and the return value is cosmetic. Return results such as
Under the provisions:
①STR1 is less than str2, return negative or-1 (VC return-1);
②STR1 equals str2, returns 0;
③STR1 is greater than str2, returns positive or 1 (VC returns 1);
The strcmp function is actually a comparison of the ASCII code of characters, and the principle of implementation is as follows: First, compare the first character of two strings, and if not equal, stop the comparison and draw the knot of two ASCII size comparisons.
If the equality is followed by the second character and then the third character, and so on. No matter what the two strings are, the strcmp function can produce results by comparing up to one of the strings with The Terminator '/0 '.[CPP] View Plain copy int strcmp (CONST&NBSP;CHAR*&NBSP;STR1,&NBSP;CONST&NBSP;CHAR*&NBSP;STR2) { assert ((str1 != null) && (str2 != null); int ret = 0; while (! ret = (unsigned char*) *str1 - (unsigned char*) *str2) && (*str2 ) { str1++; str2++; } if (ret > 0) { return 1; } else if (ret < 0) { return -1 &Nbsp; } return 0; }
strlen function : [CPP] view plain copy int strlen (const char *str) {assert (str!= NULL); int Len; while ((*str++)!= ' ") {len++; return Len; }
strcat function prototype:[CPP] View plain Copy//Add a const to the source string indicating that it is an input parameter char *strcat (Char *strdest, const char &NBSP;*STRSRC) { // return address, it cannot be placed after the assert assertion to declare address char *address = strDest; assert (strdest != null) && (strsrc != null) //to source address and destination address plus 0 Assertion while (*strdest) //is a while (*strDest Simplified form of '!= ') { //If you use while (*strdest++), you will get an error. Because Strdest also performs a ++, //at the end of the loop, Strdest will point to the next location of '. /So be in the circulation body + +, because if *strdest finally refers to //to the end of the string "". strDest++; } while (*strdest++ = *strsrc++) &NBSP;&NBSP;{&Nbsp; NULL; //The ++, can be used in the circulating condition } //Here you can add a statement *strdest= ' to '; no need return address; //to achieve chained operation, the destination address is returned to }
memcpy function:
Function: Copies n bytes from the starting position of the memory address referred to in source SRC to the starting position of the memory address referred to by Target dest.
void* memcpy (void *dest, const void* SRC, size_t count)
{
assert (dest!= null) && (src!= null)); Security Check
assert (Count > 0);
char* PSRC = (char *) src;
char* pdest = (char *) dest;
while (count--)
{
*pdest++ = *psrc++;
}
return dest;
}
Write a strcat function:[CPP] View plain copy #include <stdio.h> char* strcat (char *str1,char *STR2) { char* tempt = str1; while (*str1!= ') { str1++; } while (*str2!= ') { *str1 = *str2; str1++; str2++; } *str1 = '; ' return tempt; } int main () { char a[20] = "Hello"; char b[20] = ", world!"; printf ("%s\n", strcat (a,b)); return 0; }