Some functions of the operation string in C language

zhaozj2021-02-16  85

Many people think that the difficulty in the C language is the pointer, and the understanding of the pointer is directly related to the programs, so, some of the source code of the standard functions that the C compiler usually have, they will read them. It can be understood by the pointers and strings.

1. Strlen (), calculate the string length

Int strlen (const char string) {INT i = 0; while (string [i]) i ; returni i;} 2. Strcpy (), string copy.

Char * STRCPY (CHAR * DESTINATION, Const Char * Source) {While (* Destinaton = * Source ); Return (Destination-1);}

3. STRCAT (), the connection of the string.

Char * strcat (char * target) {char * original = target; while (* target) Target ; // Find the end of the string while (* target = * source ); returnal);} 4. Streql () determines whether the two strings are equal.

INT streql (char * str1, char * str2) {while (* str1 == * str2) && (* str1)) {str1 ; str2 ;} return (* str1 == null) && (* str2 == null ));

5. Strchr () looks for a character in a string.

Char * strchr (const char * string, int letter) {while ((* string! = letter) & (* string)) String ; return (string);}

6. chrcnt (), calculates the number of times that appears in a string in a string.

INT chrcnt (const char * string, int letter) {int count = 0; while (* string) if (* string == letter) count ; return count;}

7. Strcmp (), determine if the two strings are equal.

INT strcmp (const char * str2) {while (* str1 == * str2) && (* str1)) {str1 ; str2 ;} if ((* str1 == * str2) && (! * Str1)) // Same strings Return O; ELSE IF ((* str1) && (! * str2)) // Same But str1 lonrge RETURN-1; ELSE IF ((* str2) && (! * str1)) //// SAME BUT STR2 LONGER ELSE RETURN ((* STR1> * STR2)? - 1: 1);}

转载请注明原文地址:https://www.9cbs.com/read-12503.html

New Post(0)