Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get started with string function in C language

Shulou Source: shulou.com Published: 2022-06-02 04:32:47 10月03日 Update

This article introduces you how to get started with string functions in C language, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Realize the function of string library function

Sometimes we may be restricted from using library functions, so we need to write our own library functions. However, after understanding the functions of string library functions, it is not difficult to implement them. Today we use arrays and loops to simply implement the functions of several string library functions.

1.strlen function

If the strlen function is used to calculate the length of a string, it will stop when it encounters\ 0 and return the length of the string. Let's write our own strlen based on this feature.

Int my_strlen (char string []) {int count= 0; while (string [count]! = NULL) {count++;} return count;} / / can also change the judgment condition of while to string [count]

Some friends should know the implementation of recursion, but when it comes to the content of pointers here, I won't teach you how to implement it.

2.strcmp function

Strcmp string comparison function, which compares two strings and returns 0. If the former string is greater than the latter string, it will return a number greater than 0. If it is less than 0, it will return a number less than 0. As for the specific number returned, it depends on the compiler.

Before implementing the strcmp function, we need to understand its mechanism. Strcmp compares ASCII values character by character.

Int my_strcmp (char string1 [], char string2 []) {int I = 0; / / when neither string reaches the end, continue to compare while ((string1 [I] = = string2 [I]) & & string1 [I] & & string2 [I]) istrings; return string1 [I]-string2 [I] / / return the difference when two strings point to different characters or reach the end point}

A simple while loop can do this.

3.strcpy function

String copy function, copy the latter string to the previous string, we need to make sure that the destination is large enough

Void my_strcpy (char string1 [50], char string2 []) {int I = 0; while (string1 [iTunes +] = string2 [iTunes +]) } / / here we take advantage of the characteristic of the assignment expression to assign the value directly. When string [I] is\ 0, the assignment cycle stops automatically / / of course, this program is not perfect. According to the return value of strcpy, we can also modify char* my_strcpy (char string1 [50], char string2 []) {int I = 0; while (string1 [ionization +] = string2 [ionization +]) Return string1;} / / returns the 4.strcat function of the starting address of the destination

Strcat string append, append a new string after a string, if you understand it from the point of view of strcpy, it is to copy the string with the\ 0 position of the first string as the starting address, then it will be very simple.

Char* my_strcat (char string1 [50], char string2 []) {int I =-1; while (string1 [+ + I]); / / when the loop stops, I points to\ 0 int j = 0; while (string1 [ilevels +] = string2 [jacks +]); / / synonymous with strcpy return string1;} 5.strupr function

Strupr string uppercase function, which changes all lowercase letters in a string into uppercase, which can be easily implemented according to the ASCII value.

Char*my_strupr (char string []) {int I = 0; while (string [I]) {/ / Loop stops if when string [I] points to\ 0 (string [I] > ='a'& & string [I] ='A'& & string [I]

Tags: Characters strings functions loops functions pointing two content uppercase lowercase copy purpose language next address buddy time more features Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Shulou Information Shulou Tech Info MySQL