In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how the C language to achieve string sequential storage representation and basic operations, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!
Three storage representations of 1 string
String, that is, a string. It should be noted that there is no string data type in C language, but it is implemented as a data structure-"linear table with limited content". The concepts of empty string, blank string, length of string, substring and main string are agreed upon.
There are three main types of storage representations of strings:
[1] Sequential storage representation: use a character array (a continuous piece of memory storage space) to store the contents of a string whose maximum length has been defined and cannot be modified; (this is also a better representation of string content initially agreed upon in C language)
[2] Heap allocation storage means: use malloc and free functions to operate the heap area and dynamically allocate and release the memory in the heap area, and this storage space is used for the contents of the storage string.
[3] Block chain storage represents: in the form of a linked list, multiple string "fragments" located in different storage spaces are connected through pointers to "piece together" into a complete string
Only the first one is introduced here, that is, the sequential storage representation of the string.
Sequential storage representation and basic operation of 2 strings
Just paste the code on this part.
Because this is the operation of array elements, I am lazy and directly use some library functions provided in the C language string.h header file to simply implement some basic operations. It will be reflected when you need to write the part of the code in detail to the heap allocation implementation of the string]
2.1 header file declaration
/ * * string sequential storage implementation * / # include # define MAXLEN 255 MAXLEN+1 / structure definition-represents the string structure typedef struct {char ch [MAXLEN+1]; / / A character array of the contents of the storage string [+ 1 is to store the length of the current string} SString;/** * to store the end flag'\ 0'] copy the string T to chars * / int StrAssignS (SString* TMagar * chars) / * * get string length * / int StrLengthS (SString T); / * null operation of string * / int StrIsEmptyS (SString T); / * * concatenation operation * / int StrConcatS (SString* Techelchar * S1 charchars2); / * * intercept substring * / int SubStringS (SString* TmenSString src,int pos,int len); / * * string emptiness * / int ClearStringS (SString* T) / * * string comparison * / int StrCompareS (SString T1 Magazine SString T2)
2.2 function implementation
# include "SString.h" / * copy the string T to chars * / int StrAssignS (SString* Tzochar * chars) {/ / clear the original content memset (T-> ch,0,MAXLEN); T-> length=0; / / reassign to chars strcpy (T-> ch,chars); T-> length= (int) strlen (chars); / * printf ("len=%d\ n", strlen (T-> ch)); * / return 0 } / * null operation of string * / int StrIsEmptyS (SString T) {return T.characters 0;} / * get string length * / int StrLengthS (SString T) {return T.string;} / * * string concatenation operation * / int StrConcatS (SString* Tdome chars * S1 charr * S2) {/ / auxiliary variable char* p; / / clear the contents of T memset (T-> ch,0,MAXLEN); T-> length=0 / / concatenate the string p=strcat (s1Power2); / / concatenate the string to strcpy in T (T-> ch,p); T-> length= (int) strlen (p); return 1;} / * intercept substring * / int SubStringS (SString* TMagneSString src,int pos,int len) {/ / clear the original content memset (T-> ch,0,MAXLEN) in T; T-> length=0 / / get the substring / * * char * strncpy (char * dest, const char * src, size_t n) dest-- point to the target array src used to store the copied content-- the string n to be copied-- the number of characters to be copied from the source * / strncpy (T-> ch,src.ch+pos,len); / / reset the length of the string T-> length=len; return 1 } / * string comparison * / int StrCompareS (SString T1 Magnum SString T2) {/ * int strcmp (const char * str1, const char * str2) if the return value is less than 0, it means that str1 is less than str2. If the return value is greater than 0, it means str1 is greater than str2. If the return value is equal to 0, then str1 equals str2 * / return strcmp (T1.chCenter T2.ch). } / * string empty * / int ClearStringS (SString* T) {memset (T-> ch,0,MAXLEN); T-> length=0; return 1;}
2.3 function testing
# include # include # include "SString.h" int main (int argc,char * * argv) {SString str1,str2; / / str1.ch= "Hello,World!"; StrAssignS (& str1, "Hello,World"); StrAssignS (& str2, "Hello"); printf ("len=%d\ n", StrLengthS (str1); / / string concatenation StrConcatS (& str1, "SHello", "Hi"); printf ("len=%d\ n", StrLengthS (str1) / / print string content printf ("str1=%s\ n", str1.ch); printf ("str2=%s\ n", str2.ch); printf ("CompareRes=%d\ n", StrCompareS (str1,str2)); / / intercept substring SubStringS (& str2,str1,2,3); puts (str1.ch); puts (str2.ch); / / clear string content ClearStringS (& str1) Printf ("str1's len=%d,str2's len=%d,\ nso str1 isEmpty:%d,str2 isEmpty:%d\ n", StrLengthS (str1), StrLengthS (str2), StrIsEmptyS (str1), StrIsEmptyS (str2)); return 0;}
Attach a screenshot of the test results:
The above is all the contents of the article "how to realize the sequential storage representation and basic operation of strings in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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.
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.