Length-Constrained string functions : This function takes a display length as a parameter to limit the number of characters to compare or copy, preventing some long strings from spilling from the target array.
The most common restricted string functions in three:
char * strncpy (char *dst,char const *src,size_t len);
strncpy copy len characters from Src to DST, if strlen (SRC) is shorter than Len, the extra characters are filled with NUL, note that when strlen (SRC) is greater than or equal to len,strncpy does not end with NUL, So be careful when using strncpy.
Implementation of the strncpy function:
#include <stdio.h> #include <stdlib.h> #include <assert.h>char * my_strncpy (char * Dst, char const *src, int len) { assert (Dst != NULL); assert (src != null); char *p = dst; while (len--) { if (*src != ' ) *p++ = *src++; else *p = ' + ' ; } RETURN&NBSP;DST;} Int main () { int len = 0; char arr1[100] ; &NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;ARR2[100];&NBSP;&NBSP;&NBSP;&NBSP;SCANF ( "%s%s%d" , arr1, arr2,&len); char *ret=my_strncpy (Arr1, arr2, len); printf ( "%s\n" ,ret) system ( "pause" ); return 0;}
char * STRNCAT (char *dst,char const *src,size_t len);
Similarly, strncat copies the Len characters in Src to DST at most, and ends with NUL, but it does not populate with nul like strncpy, and Strncat does not consider whether the remaining space in the DST array is sufficient to lay down Len characters.
Implementation of the STRNCAT function:
#include <stdio.h> #include <stdlib.h> #include <assert.h>char * my_strncat (char * Dst, char const *src, int len) { assert (dst != NULL); char *p = dst; while (*p! = ' ) { p++; } while (len--) { if (*p++= *src++) ; else break ; } if (len==-1) *p = ' + ' ; returN&NBSP;DST;} Int main () { char arr1[100]; char arr2[100]; INT&NBSP;LEN&NBSP;=&NBSP;0;&NBSP;&NBSP;&NBSP;SCANF ( "%s%s%d" , arr1, arr2, &len); char *ret = my_strncat (Arr1, arr2, len); printf ( "%s\n" , ret) system ( "pause" ); return 0;}
int strncmp (char const *dst,char const *src,size_t len);
The only difference between strncmp and strcmp is that strncmp is a maximum of Len characters and is exactly the same as the return value.
STRNCMP Feature Implementation:
#include <stdio.h> #include <stdlib.h> #include <assert.h>int my_strncmp (char const *dst, char const *src, int len) { assert (dst != null); assert (src != null); while (len) { if (*DST&NBSP;==&NBSP;*SRC) { if (*src == ' ) return 0; dst++; src++; } else if ( *DST&NBSP;>&NBSP;*SRC) return 1; else return -1; } return 0;} Int main () { char arr1[100]; char arr2[100]; &NBSP;&NBSP;&NBSP;INT&NBSP;LEN&NBSP;=&NBSP;0;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ( "%s%s%d" , arr1, arr2, &len); &NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;RET&NBSP;=&NBSP;MY_STRNCMP (arr1, arr2, Len); if (ret == 1) printf ( "arr1[]>arr2[]\n" ); else if (ret == 0) printf ( "arr1[]=arr2[]\n" ); else if (ret==-1) prinTF ( "arr1[]<arr2[]\n" ); system ( "pause" ); return 0;}
By the way, STRPBRK and Strrstr are two functions:
STRPBRK prototype:Char *strpbrk (char const *dst,char const *group);
Features: finds the first occurrence of any character in the group in DST and returns this address .
STRPBRK feature Implementation :
#include <stdio.h> #include <stdlib.h> #include <assert.h>char * my_strpbrk (char CONST&NBSP;*DST,&NBSP;CHAR&NBSP;CONST&NBSP;*SRC) { assert (Dst != NULL); assert (src != null); char *p1 = dst; while (*p1!= ' ) { char *p2 = src; while (*p2!= ' ) { if (*p1 == *P2) return p1; p2++; } p1++; } return null;} Int main () {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;ARR1[100];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;ARR2 [20]; scanf ( "%s%s" &NBSP;,&NBSP;ARR1,&NBSP;ARR2); &NBSP;CHAR&NBSP;*RET=MY_STRPBRK (ARR1,ARR2); printf ( "% #p \ n" , &*ret); system ( "pause" ); return 0;}
Refer to STRRCHR This function to implement a STRRSTR function that is not in a library.
function : The only difference between STRRSTR and strstr is that STRSTR returns where the substring first appears, and Strrstr returns the last address that appears.
#include <stdio.h> #include <stdlib.h> #include <assert.h>char * my_strrstr (char CONST&NBSP;*DST,&NBSP;CHAR&NBSP;CONST&NBSP;*SRC) { char *last = null; assert (dst!=null); while (*dst != ' ) { if (*dst == *src ) { char *p1=dst+1; char *p2 =src+1; while ( *P1==*P2) { if (*p2 == ' ) { last = dst; break ; } p1++; p2++; } if (*p2 == ' ) last= dst; } dst++; } return last;} Int main () { char arr1[100]; char arr2[50]; &NBSP;&NBSP;&NBSP;SCANF ( "%s%s" , arr1, arr2); char *ret=my_ Strrstr (ARR1,&NBSP;ARR2); printf ( "% #p \ n" , &*ret); system ( "pause" ); return 0;}
This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1759874
Length-constrained string functions and other string functions