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

Analysis on the Operation of stacking string examples in C language

2025-01-18 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 the operation and analysis of C language stacking examples. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

I. the concept of stacking string

Similar to the fixed-length sequential storage structure, the character sequence of the string is stored in a set of consecutive memory cells, except that the storage space of the heap string is allocated dynamically, as long as the storage space is allocated successfully. You won't worry about the truncation of the string during insertion or connection.

The three functions malloc (), free () and realloc () are used to operate on dynamic storage.

Basic operation: # include#include#include#define false 0#define true 1typedef struct {char * ch; / / character array. If it is not empty, point to the starting address. If it is empty, NULL int len; / / length} HString;// initializes int HInit (HString * s) {s-> ch=NULL; s-> len=0;} / / string assignment int HStrAssign (HString * s Const char * chars) {int item0 While (chars [I]! ='\ 0') {/ / determine the length of iChars;} s-> len=i; if (s-> chestnut null) {free (s);} else {s-> ch= (char *) malloc ((s-> len+1) * sizeof (char)) / / Unit 0 does not need if (s==NULL) {printf ("space application failed!") ; return false;} for {/ / assign s-> ch [I] = chars [I-1];} / / string traversal int HSbianli (HString * s) {if (s-> len==0) {printf ("string empty!") ; return false;} else {int i; for ("% c", s-> ch [I]) {printf (% c, s-> ch [I]);} return true }} / / string insertion into int HStrInsert (HString * s pos,const HString t) {if (poss- > len) {printf ("invalid position!") ; return false;} char * temp; temp= (char *) malloc ((s-> len+t.len+1) * sizeof (char)); int i; for (iFibich [I];} for (itransipositich [i+pos];} free (s-> ch); s-> ch=temp; s-> len=s- > len+t.len; return true) } / / string deletion int HStrDelete (HString * sreint pos,int len) {if (s-> len==0) {printf ("string empty!") ; return false;} else if (len > = s-> len) {printf ("illegal location!") ; return false;} else {for (int ipositon postion postion +) {/ / Span assigns s-> ch [I] = s-> ch [i+len];} s-> len=s- > len-len; return true }} / / string connection int HStrCon (HString * s char Const HString t) {s-> ch= (char *) realloc (s-> ch, (s-> len+t.len+1) * sizeof (char)); if (s==NULL) {printf ("space application failed!") ; return false;} if (s-> len==0) {printf ("string empty!"); return false;} else {for (int irid1; icht [I + s-> len] = t.ch [I];} s-> len=s- > len+t.len; return true }} / / find the string int HStrchild (HString * s pos,int len) {if (s-> len==0) {printf ("string empty!") ; return false;} else {for (int iTuno [i+pos]);} return true;}} int main () {HString s; HString S1; HInit (& s); HInit (& S1); char a [] = {"aaaaa"}; char b [] = {"bbb"} HStrAssign (& SMagna); HStrAssign (& s1Powerb); printf ("- assign aaaaa to sGrammer BBB assign value to s1 -\ n"); printf ("string s is:"); HSbianli (& s); printf ("\ nstring S1 is:"); HSbianli (& S1); printf ("\ nConnect S1 to s is:") HStrCon (& s); HSbianli (& s); printf ("\ ninsert the string S1 into the second position of the s string:"); HStrInsert (& sPower2); HSbianli (& s); printf ("\ nDelete the inserted S1 string:"); HStrDelete (& sMagne2); HSbianli (& s) Printf ("\ ns1 in the substring of length 2 of the second element is:"); HStrchild (& sMagne2);} 3. Run:

The code can be run.

These are all the contents of the article "Analysis of the Operation of stacking string examples in C language". Thank you for your 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