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 C language to simulate and realize string function

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

Share

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

In this issue, the editor will bring you about how to simulate the string function in C language. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Preface

Some string functions are often used in the programming process, these string functions are in the C language standard library, we can use them directly. But we also need to understand how they are implemented.

Analog 1.strlen function

The strlen function is used to find the length of a string. The official explanation is shown in the picture.

The return value type is an unsigned integer and the parameter type is char*, meet\ 0 stop.

There are three methods to realize the simulation of strlen: 1. Count counting. 2. Recursion. 3. Pointer minus pointer.

Count count

# include int my_strlen (const char* str) {int count = 0; while (* str++) {count++;} return count;} int main () {char str [] = "abcdefg"; int len = my_strlen (str); printf ("% d", len); return 0;}

Recursion

# include int my_strlen (const char* str) {if (* str = ='\ 0') {return 0;} else {return my_strlen (+ + str) + 1;}} int main () {char str [] = "abcdefg"; int len = my_strlen (str); printf ("% d", len); return 0;}

Pointer minus pointer

# include int my_strlen (const char* str) {char* p = str; while (* p) {pendant;} return p-str;} int main () {char str [] = "abcdefg"; int len=my_strlen (str); printf ("% d", len); return 0;} 2.strcpy function

The strcpy function is a string copy function. Is to copy the source address string to the destination address. The source address string must have\ 0. The destination address must be large enough. \ 0 of the source address string is also copied to return the address of the destination address.

The official explanation is shown in the picture.

Simulation realization

# include char* my_strcpy (char* dest,const char* sour) {char* pumped destinations; while (* dest++ = * sour++) {;} return p;} int main () {char sur [] = "abcdefg"; char dest [10] = {0}; printf ("% s", my_strcpy (dest,sur)); return 0;} 3.strcat function

The strcat function is a string append function. Is to append the source address string to the destination address. It is important to note that when you encounter\ 0 of the source address, the destination address must be modifiable and large enough. The strcat function cannot be used to append the string itself, it will cross the line and won't stop at all.

The official explanation is shown in the picture.

Simulation realization

# include char* my_strcat (char* str2,const char* str1) {char* p = str2; while (* str2++) {;} str2--; while (* str2++ = * str1++) {;} return p;} int main () {char str1 [] = "abcdefg"; char str2 [20] = "abc" Printf ("% s", my_strcat (str2, str1)); return 0;} 4.strcmp function

The strcmp function is a string comparison function, which returns a positive value if str1 is larger than str2, and a negative value when str1 is smaller than str2. Str1=str2 compares the next pair of characters. Stop as soon as one meets\ 0.

The official explanation is shown in the picture.

Simulation realization

# include int my_strcmp (const char* str1,const char* str2) {while (* str1++-*str2++==0 & & * str1Christ stories'\ 0' & & str2percent stories'\ 0') {;} return *-str1-*-str2;} int main () {char str1 [] = "abcdefg"; char str2 [] = "bbcdef"; int a=my_strcmp (str1,str2) Printf ("d", a); return 0;} 5.strncpy function

The strncpy function is a string copy function with a limited length, which can specify how many bytes to copy. When the number of bytes given is greater than the string to be copied, it is not enough to supplement with\ 0.

Official explanation

Simulation realization

# include char* my_strncpy (char* dest,const char* sour,int num) {char* p = dest; while (num--) {if (* sour! ='\ 0') {* dest++ = * sour++;} else {* dest++ ='\ 0' }} return p;} int main () {char sur [] = "abcdefg"; char dest [10] = {0}; printf ("% s", my_strncpy (dest, sur,10)); return 0;} 6.strncat function

Strncat function, it is important to note that the destination address slash zero is added after the string append is completed. So it can realize that it can add to itself.

Official explanation

Simulation realization

# include char* my_strncat (char* str2,const char* str1,size_t num) {char* p = str2; while (* str2++) {;} str2--; while (num--) {if (* str1! ='\ 0') {* str2++ = * str1++ } * str2 ='\ 0mm; return p;} int main () {char str1 [] = "abcdefg"; char str2 [20] = "abc"; printf ("% s", my_strncat (str2, str1,6)); return 0;} 7.strncmp function

The strncmp function, which compares strings, is similar to strcmp except that you can be sure to compare the first few characters.

Official explanation

Simulation realization

# include int my_strncmp (const char* str1,const char* str2,size_t num) {while (num--&& * str1++-*str2++==0 & & * str1customers'\ 0stories & str2bands'\ 0') {;} return (*-str1-*-str2);} int main () {char str1 [] = "abcdefg"; char str2 [] = "bbcdef" Int a=my_strncmp (str1,str2,5); printf ("d", a); return 0;} 8.strstr function

The strstr function is to find a substring in a string.

Official explanation

Simulation realization

# include const char* my_strstr (const char* str1, const char* str2) {while (* str1) {const char* p1 = str1; const char* p2 = str2; while (* p1room+ = * p2room+) { } if (*-- p2 = ='\ 0') {break;} else {str1++;}} return str1;} int main () {char str1 [] = "abbbcdefg"; char str2 [] = "bcde" Int len = strlen (str2); const char* p = my_strstr (str1, str2); while (len--) {printf ("% c", * paired +);} return 0;} 9.strtok function

The strtok function is a string cutting function, which is less used and more difficult to understand. It is time to give a collection of characters that are used as delimiters. The strtok function finds the delimiter and sets it to 0. 0. Returns a pointer to the tag. If the first parameter is not empty, strtok finds the first delimiter, sets it to zero and records the address there, and the second call to strtok passes a NULL and strtok looks for the next delimiter at the previous'0'.

Official explanation

Simulation realization

# include # include char* my_strtok (char* str, char* demion) {static char* p_last = NULL; if (str = = NULL & & (str = p_last) = = NULL) {return NULL;} char* s = str; char* t = NULL; while (* s! ='\ 0') {t = demion While (* t! ='\ 0') {if (* s = = * t) {p_last = s + 1; if (s-str = = NULL) {str = pendant; break Return NULL;} int main (void) {char str [] = "liusen,lin,lll"; char* result = NULL; result = my_strtok (str, ","); printf ("% s\ n", result) Result = my_strtok (NULL, ","); printf ("% s\ n", result); return 0;} above is the editor's share of how to use C language to simulate and implement string functions. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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