Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use character function and string function in C language

Shulou Source: shulou.com Published: 2022-06-02 03:35:02 10月21日 Update

This article mainly explains "how to use the character function and string function of C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn how to use the character function and string function of C language.

First, the character & string function 1.strlencolor-find the length of the string

Explanation: it is the number of characters before the required'\ 0'. Library function type-size_t (const char*) size_t = = unsigned int

# include int main () {char arr [] = "hello world"; printf ("% d\ n", strlen (arr)); return 0;}

Unlimited length restricted strcpystrncpystrcatstrncatstrcmpstrncmp2.1.strcpy-- copy string

Library function type-char* (char*, const char*)

Source string-copied string

Explanation:'\ 0' is a condition for the copy string to terminate the copy, and there must be enough space in the destination space to put down the source string.

# include int main () {char arr1 [20] = {0}; char arr2 [] = "hello world"; strcpy (arr1,arr2); printf ("% s\ n", arr1); return 0;}

2.2.strcattel-append string / link string

Library function type-char* (char*, const char*)

Explanation: append the source characters by finding the'\ 0characters'in the target string

# include int main () {char arr1 [20] = "hello"; char arr2 [] = "world"; printf ("% s\ n", strcat (arr1,arr2)); return 0;}

2.3.strcmpMui-string comparison

Library function type-int (const char*, const char*)

Compare the left string with the right string: less than return less than 0, equal to return 0, greater than return greater than 0.

Comparison method: compare two strings from left to right, one character to another, and compare them according to the corresponding ascll code.

Equal to the situation

# include int main () {char arr1 [] = "abc"; char arr2 [] = "abc"; printf ("% d\ n", strcmp (arr1,arr2)); return 0;}

The case of being greater than

# include int main () {char arr1 [] = "abc"; char arr2 [] = "abd"; printf ("% d\ n", strcmp (arr1,arr2)); return 0;}

A situation that is less than

# include int main () {char arr1 [] = "abd"; char arr2 [] = "abc"; printf ("% d\ n", strcmp (arr1,arr2)); return 0;}

2.4.strncpymuri-restricted string copy

Library function type-char* (char*, const char*,size_t)

Size_t = = unsigned int

You can choose the number of characters you want to copy.

Note: if the number of characters you choose to copy is greater than the length of the source character, the extra part will be copied as'\ 0'.

# include int main () {char arr1 [20] = "abd"; char arr2 [] = "hello world"; printf ("% s\ n", strncpy (arr1,arr2,5)); return 0;} 2.5.strncatmer-restricted connection string

Library function type-char* (char*,const char*,size_t)

Explanation: you can choose the number of characters you want to append / connect.

Note: if the length of the appended character is greater than the length of the source string, the throw is stopped at'\ 0'. If the length of the source character is insufficient, a'\ 0' is automatically appended.

# include int main () {char arr1 [20] = "abd"; char arr2 [] = "hello world"; printf ("% s\ n", strncat (arr1,arr2,5)); return 0;} 2.6.strncmpmuri-restricted comparison string

Function library type-int (const char*,const char*,size_t)

Explanation: you can choose the character length you want to compare

# include int main () {char arr1 [20] = "abd"; char arr2 [] = "hello world"; printf ("% d\ n", strncmp (arr1,arr2,5)); return 0 } Thank you for your reading. The above is the content of "how to use the character function and string function of C language". After the study of this article, I believe you have a deeper understanding of how to use the character function and string function of C language. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Character string function copy length type language number situation selection learning content goal space two namely idea request article way Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology macOS vpn Apple