C語言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實(shí)例代碼
創(chuàng)新互聯(lián)公司是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注做網(wǎng)站、網(wǎng)站制作、網(wǎng)絡(luò)營銷、企業(yè)網(wǎng)站建設(shè),賣鏈接,1元廣告為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計(jì)到用戶體驗(yàn)提高,創(chuàng)新互聯(lián)力求做到盡善盡美。
輸出結(jié)果:
實(shí)現(xiàn)代碼:
#includeusing namespace std; typedef int Status; #define Max 20 #define OK 1 #define ERROR 0 #define OVERLOE -2 typedef struct//堆分配表示串 { char *ch; int length; }HString; //=================================================================== Status CreatHString(HString &H)//構(gòu)造字符串 { H.length = 0; H.ch = (char *)malloc(Max*sizeof(char)); for (int i = 0; i < Max; i++) { H.ch[i]=getchar(); H.length++; if (getchar() == '\n') break; } return OK; }//CreatHString //=================================================================== Status PrintHString(HString H)//輸出所輸入的字符串 { if (H.length == 0) { cout << "空串!" << endl; return ERROR; } else for (int i = 0; i < H.length; i++) cout << H.ch[i] << " "; cout << endl; return OK; }//PrintHString //=================================================================== Status HStringLength(HString H)//求字符串的長度 { cout << "您輸入的字符串長度為:" << endl; cout << H.length << endl; return OK; }//HStringLength //=================================================================== Status HStringCompare(HString H, HString T)//求兩個(gè)字符串長度差(絕對值) { cout << "兩個(gè)字符串的長度差為:" << endl; int L; L = H.length - T.length; if (L<0) cout << -L << endl; if (L>=0) cout << L << endl; return OK; }//HStringCompare //=================================================================== Status ConcatHString(HString &S, HString H, HString T)//鏈接H和T { if (!(S.ch = (char *)malloc((H.length + T.length)*sizeof(char)))) exit(OVERLOE); for (int i = 0; i < H.length; i++) S.ch[i] = H.ch[i]; S.length = H.length + T.length; for (int j = H.length; j < S.length; j++) S.ch[j] = T.ch[j-H.length]; return OK; }//ConcatHString //=================================================================== Status SubHString(HString &Sub, HString S, int pos,int len) {//用Sub返回串S的第pos個(gè)字符起長度為len的子串 if (pos<1 || pos>S.length) { cout << "輸入的位置有誤!" << endl; return ERROR; } if (len<0 || len>S.length - pos + 1) { cout << "輸入的長度有誤!" << endl; return ERROR; } if (!len) { Sub.ch == NULL; Sub.length = 0; } else { Sub.ch = (char *)malloc(len*sizeof(char)); for (int i = 0; i < len ; i++) Sub.ch[i] = S.ch[pos + i - 1]; Sub.length = len; } return OK; }//SubHString //=================================================================== Status ClearHString(HString &H)//將H清為空串 { if (H.ch) { free(H.ch); H.ch = NULL; } H.length = 0; return OK; }//ClearHString //=================================================================== void main() { HString S,H,T; cout << "請輸入一個(gè)字符串(按回車鍵結(jié)束):" << endl; CreatHString(H); cout << "現(xiàn)在串中的字符為:" << endl; PrintHString(H); HStringLength(H); cout << "請?jiān)佥斎胍粋€(gè)字符串(按回車鍵結(jié)束):" << endl; CreatHString(T); HStringCompare(H, T); ConcatHString(S, H, T); cout << "現(xiàn)在串中的字符為:" << endl; PrintHString(S); HString Sub; int pos, len; cout << "請輸入截取位置pos及長度len:" << endl; cin >> pos >> len; SubHString(Sub, S, pos, len); cout << "截取的子串為:" << endl; PrintHString(Sub); ClearHString(S); cout << "檢驗(yàn)S清空后是否為空:" << endl; PrintHString(S); }//main