Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the string functions of Cstroke + and how to use them?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article, "what are the string functions of CAccord + and how to use them?" so the editor summarizes the following, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what are the string functions and how to use them?"

1. Strrchrchar * strrchr (const char * str, int c)

Search for the position of the last occurrence of the character c (an unsigned character) in the string pointed to by the parameter str.

Parameters.

Str-- C string.

C-the character to search for. Passed in int form and eventually converted back to char form.

Return value

This function returns the position of the last occurrence of the character c in str. If the value is not found, the function returns a null pointer.

Example # include # include int main () {int len; char str [] = "www.baidu.com"; char ch ='.'; char* ret; ret = strrchr (str, ch); printf ("string after% c is% s\ n", ch, ret); return 0;} output

2. Atoiint atoi (const char * str)

Converts the string pointed to by the parameter str to an integer (of type int).

Parameters.

Str-the string to convert to an integer.

Return value

This function returns a long converted integer or zero if no valid conversion is performed.

Example # include # include # include int main () {int val; char str [20] = "20220228"; val = atoi (str); printf ("string value =% s, integer value =% d\ n", str, val); strcpy (str, "hhhhh"); val = atoi (str); printf ("string value =% s, integer value =% d\ n", str, val) Return 0;} output

3. Strcatchar * strcat (char * dest, const char * src)

Append the string pointed by src to the end of the string pointed to by dest.

Parameters.

Dest-points to the target array, which contains a C string and is sufficient to hold the appended string.

Src-- points to the string to be appended, which does not overwrite the target string.

Return value

This function returns a pointer to the final target string dest.

Example # include # include int main () {char src [50] = "This is source", dest [50] = "This is destination"; strcat (src, dest); printf ("% s", src); return 0;} output

4. Strstrchar * strstr (const char * str1, const char * str2)

Used to determine whether the string str2 is a substring of str1. If so, the function returns the address where str2 first appeared in str1; otherwise, it returns NULL.

Parameters.

The C string of the str1-- to be retrieved.

The small string that str2-- searches for within the haystack string.

Return value

This function returns the location where the str2 string first appeared in str1, or null if it is not found.

Example # include # include int main () {char str1 [20] = "www.baidu.com"; char str2 [10] = ".baidu"; char* ret; ret = strstr (str1, str2); printf ("% s", ret); return (0);} output

Strncasecmpint strncasecmp (const char * S1, const char * S2, size_t n)

Function under Linux, comparing the first n characters of arguments S1 and S2 strings, and ignoring character case

Parameters.

S1: string 1

S2: string 2

Len: maximum number of characters compared

Return value

Return 0 if S1 and S2 match (equal); return a value greater than 0 if S1 is greater than S2; return a value less than 0 if S1 is less than S2

6. Strncpychar * strncpy (char * dest, const char * src, size_t n)

Copy the string pointed by src to dest, with a maximum of n characters. When the length of the src is less than n, the rest of the dest is filled with empty bytes.

Parameters.

Dest-points to the target array used to store the replicated content.

Src-- the string to copy.

N-the number of characters to copy from the source.

Return value

This function returns the final copied string.

Example # include # include int main () {char src [40] = "i am src hh."; char dest [12]; memset (dest,'\ 0mm, sizeof (dest)); strncpy (dest, src, 10); printf ("% s\ n", dest); return 0;} output

7. Strcspnsize_t strcspn (const char * str1, const char * str2)

The retrieval string str1 begins with several consecutive characters that do not contain characters in the string str2.

Parameters.

Str1-the C string to be retrieved.

Str2-- this string contains a list of characters to match in str1.

Return value

This function returns the number of characters with no characters in the string str2 at the beginning of the str1.

Example: # include # include int main () {int len; char str1 [] = "www.baidu.com"; char str2 [] = "bc"; len = strcspn (str1, str2); printf ("first matching character in% d", len); return (0);} output

The above is about the content of this article on "what are the string functions and how to use them?". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report