The implementation of strlen of strcpy of of strcmp of function in C language

  • 2020-05-27 06:33:51
  • OfStack

strlen function prototype: unsigned int strlen(const char *); Returns the number of characters before the first \0 in the string.

1. Prototype strcat function char* strcat(char* dest,const char* src); Concatenate the string by concatenating the second string to the place where the first occurrence of \0 begins in the first string. Returns the first address of the concatenated character. Does not check the size of the first array to hold the second string. If the allocated memory of the first array is insufficient to accommodate the second string, the extra characters will overflow into the adjacent memory unit.

2. Prototype of strncat function: strncat(dest,src,maxsize) The function is similar to strcat1, but it takes a parameter of maxsize and sets the maximum length of characters to hold. If the maximum character length is reached before \0 is encountered, only the maximum number of characters will be concatenated.

3. Prototype strcpy function char* strcpy(char* dest,const char* src); Copies the characters before the second string \0 into the first memory address. Returns the first address of the copied string. The return value of char* is there for the function to support chained expressions, which adds to the "added value" of the function. char a[7]="abcdef",char b[5]="xyz";

The strcpy(a,b) function assigns the following array to the first one. After the five elements have been removed, the element starting with the subscript 5 is still the element of the previous a[5].

4. strncpy(str1,str2,numbe) The function assigns the first number characters in str2 to str1, or the first str1 characters before \0.

5. Prototype strcmp function int strcmp(const char *src1,const char* src2); Compare the ASCII code from the first of the two strings. If the former is greater than the latter and returns 1, and if the former is less than the latter and returns -1, then 0 is returned. When src or src meet \0, the comparison stops.strcmp compares strings, not characters, and the comparison between characters can be directly used by ==

6. strncmp(str1,str2,numbe) The function adds an int parameter to strcmp, specifying whether the previous characters are equal.

Note: there is no bound size for the strcat and strcpy functions, so make sure you have enough memory when you use them.

For memory problems: it is safer to use the memcpy function.

memcpy function prototype void * memcpy(void *desc,const char* src,unsigned int count); In contrast to the strcpy function, the memcpy function does not copy the characters before \0 to desc, but copies the first count characters.

memcmp function, function prototype: int memcmp(void *buf1, void *buf2, unsigned int count); It also compares the count characters before the two strings.


unsigned int strlenght(const char* src)
 unsigned int len=0;
 return len;
char* strlink(char* dest,char* src)
 char *tmp=dest;
 assert((dest!= NULL)&&(src!=NULL));
  return tmp;
char* strcopy(char* dest,const char* src)
 char *tmp=dest;
 return tmp;
int strcompare(const char* src1,const char* src2)
 int x=0;
 while(!(x = *src1-*src2) && *src1)
  x = 1;
  x = -1;
 return x;
void main()
 char arr[100] = "It's wonderful weather!!";
 char arr1[20] = "I am\0 fine!";
 char *arr2;
 printf("%s For the length of the :%d\n",arr,strlenght(arr));
 printf("%s For the length of the :%d\n",arr1,strlenght(arr1));
 arr2 = strlink(arr,arr1);
 printf("%s %s\n %d\n",arr1,arr,strcompare(arr1,arr));


Related articles: