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 strlen function in C language

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

Share

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

Today, the editor will share with you the relevant knowledge points about how to use the strlen function in C language. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

The string functions in C language are as follows

Get string length

Strlen

String function with unlimited length

Strcpy

Strcat

Strcmp

String function with limited length

Strncpy

Strncat

Strncmp

String lookup

Strstr

Strtok

Error message report

Strerror

Let's see how to implement them.

Get string length strlen

Let's see what the document says, as follows

Strlen document

Size_t strlen (const char * str)

Get string length

Get string length

Returns the length of the C string str.

Returns the length of the C string str

The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

The length of the C string is determined by'\ 0', that is, the length is calculated from the first beginning of the string as soon as it encounters'\ 0' (excluding'\ 0')

This should not be confused with the size of the array that holds the string. For example:

Don't be confused about the size of the array you create, like this

Char mystr [100] = "test string"

Defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof (mystr) evaluates to 100, strlen (mystr) returns 11.

Define an array mystr of size 100, and then mystr has been initialized to a string of length 11. So, sizeof (mystr) gets 100, and strlen (mystr) returns 11. 0.

To sum up, we can know

The string has'\ 0' as the closing flag, and the strlen function returns the number of characters that appear before'\ 0' in the string (excluding'\ 0').

The function only recognizes'\ 0', and the string pointed to by the parameter must end with'\ 0'.

Note that the return value of the function is size_t, which is unsigned

Realize

There are several implementations of the strlen function.

such as

The method of counter

Recursion

-pointer.-pointer.

And then one by one.

1. Counter: use a variable to record-count

It is a good habit to assert that the pointer is not empty.

Int my_strlen (char* str) {int count = 0; assert (str! = NULL); while (* str! ='\ 0') / while (* str) {count++; str++;} return count;}

Just keep looking for'\ 0'. When * str is not'\ 0', count++,str++, until you encounter'\ 0', and then return count is the length.

two。 Recursion

It is a good habit to assert that the pointer is not empty.

Int my_strlen (char* str) {assert (str! = NULL); char* p = str; while (* p = ='\ 0') {return 0;} return 1 + my_strlen (p + 1);}

For example, the incoming str address is 1000

So in 1 + my_strlen (p + 1), p + 1, the pointer is offset to 1001, and so on.

1 + 1 + my_strlen (p + 1)

1 + 1 + 1 + my_strlen (p + 1)

1 + 1 + 1 + 1 + my_strlen (p + 1)

...

1 + 1 + 1 + 1 +. + 0

You can finally get the length.

3. -pointer.-pointer.

It is a good habit to assert that the pointer is not empty.

Int my_strlen (char* str) {assert (str! = NULL); char* p = str; while (* p! ='\ 0') {pairing;} return p-str;}

Assign the address of the pointer str to a new pointer, pcenture, as the pointer to the starting address, without changing it, record the starting address.

Then use the pointer p to find'\ 0 'characters, determine whether the current character is'\ 0', not just padded characters, and then continue to determine the next character, so loop until the pointer p finds'\ 0', and then use the current pointer p minus the starting pointer str to return, which is the length.

These are all the contents of the article "how to use strlen functions in C language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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