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

How to use string function in C language

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use the C language string function, the article is very detailed, has a certain reference value, interested friends must read it!

I. strlen function simulation

The string ends with'\ 0', and the strlen function returns the number of characters that precede'\ 0' in the string (excluding'\ 0')

There are three ways to do this:

① counter mode

② cannot create temporary variable counters

③ pointer-pointer

Method 1: counter mode

Int my_strlen (const char* str) {int count = 0 str / define count is used to record the number of characters in the string array while (* character) {count++; str++;} return count;}

Method 2: cannot create temporary variable counters

Int my_strlen1 (const char* str) / / recursively completes the simulation of string length function {if (* str = ='\ 0') return 0; return 1 + my_strlen1 (str + 1);}

Method 3: pointer-pointer

The number of elements between the two pointers is obtained by subtracting the pointers, which can be used to simulate the strlen function.

Int my_strlen2 (char* s) / / static const {char* p = s; while (* p! ='\ 0') paired signals; return p-s;} cannot be used when simulating in this manner.

Test sample

two。 Strcpy function simulation

Copy the string pointed to by src including'\ 0' to the string pointed to by dest

The source string (the string pointed to by src) must end with'\ 0'

The target space needs to be large enough to ensure that the source string can be stored

Int main () {char s [10] = {'a 'axiomagem, my_strcpy (s, S1); printf ("% s", my_strcpy (s, S1)); / / copy characters from S1 to s space return 0 } char* my_strcpy (char* dest, const char* src) {char* ret = dest; assert (dest! = NULL); / / assertion assert (src! = NULL); / / assertion while ((* dest++ = * src++)); return ret;}

Test result

Finally, the string with the Terminator'\ 0' in S2 is copied to s.

3. Strcat function simulation

Copy the string pointed to by src including'\ 0' to the string pointed to by dest

The source string must end with'\ 0'

The target space * dest needs to be large enough to ensure that the source string can be stored (* src)

Char* my_strcat (char* dest, const char* src) {char* ret = dest; assert (dest! = NULL); assert (src! = NULL); while (* dest) / / access to the last character position of the string pointed to by dest {dest++;} while ((* dest++ = * src++)) / / copy the string pointed to by src to the following return ret;} of the string pointed to by dest

Test sample

Copy the string pointed by src to the string pointed to by dest, and finally get the result.

IV. Strcmp function simulation

The strcmp function compares strings, using strcmp (string 1, string 2), which compares the two strings:

If string 1 equals string 2 the strcmp function returns 0

If string 1 is less than string 2, the function strcmp returns a negative value

If string 1 is greater than string 2, the string strcmp function returns a positive value.

The specific implementation is as follows:

Int my_strcmp (const char* dest, const char* src) {int ret = 0; assert (src! = NULL); assert (dest! = NULL); while (! (ret = * (unsigned char*) src-* (unsigned char*) dest) & * dest) {+ src; + + dest;} if (ret

< 0) ret = -1; else if (ret >

0) ret = 1; return ret;} these are all the contents of the article "how to use string functions in C language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to 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