In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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]
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.