21:32:23 2019-06-01
Today a moment to review the C string functions, string functions the way to talk about the algorithm
First emphasize string functions in <string.h> header file
1.strcmp function
Usage: int strcmp (char * str1, char * str2);
Function: string comparison, equal 0 and does not equal the first value returns the string of characters does not match the difference (a str1-str2, difference values: i.e. the difference in ASCII)
Algorithm
int mystrcmp ( char * P1, char * P2) { // * P1, P2 * for storing strings str1, str2 first address (i.e. character array name) int I = 0 ; the while (P1 [I] == ! P2 [I] && P1 [I] = ' \ 0 ' ) // start comparing a character from the first, if not terminated and equal, straight ahead { I ++ ; } int NUM; // to represent a return value IF (P1 [I] == ' \ 0 ' && P2 [I] == ' \ 0 ' ) // where the end of the two strings { NUM =0 ; // is determined to be equal to } the else { NUM = [I] P1 -P2 [I]; // tell the difference ASCII character codes assigned to the NUM } return NUM; }
2.strchr function
用法:char *strchr(char *str, char c);
Function: Where to Find in a given character string of the first occurrence, if there returns NULL, otherwise it returns a pointer to the first occurrence location
Algorithm:
char * mystrchr ( char * STR, char C) { IF (STR == NULL) { return NULL; } the else { (whlie ! * STR = ' \ 0 ' ) { IF ((* STR) == CH) // equivalent to * str str [i], which is a character string { return STR; BREAK ; // is equal to the return address and the loop is exited } STR ++ ; } return NULL; } }
3.strcat function
Usage: char * strcat (char * destin, char * source);
Function: string concatenation function, the successful return of the first address, otherwise returns NULL
Algorithm:
char * mystrcat ( char * str1, char * str2) { IF (str2 str1 == NULL || == NULL) return NULL; char * P = str1; // save the pointer first address str1 the while (* str1 =! ' \ 0 ' ) { str1 ++ ; } the while (* str2 =! ' \ 0 ' ) { * * str1 = str2; str1 ++ ; } * str1 = ' \ 0 ' ; return the p-; // return the first address }
Speaking today on the first here, continue tomorrow!
Do not rely too much on library functions, be sure to achieve the realization of yourself, for your understanding and the growth of great help.