输出结果:
实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#include<iostream> using 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) //构造字符串 { 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) //求两个字符串长度差(绝对值) { cout << "两个字符串的长度差为:" << 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个字符起长度为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 << "请输入一个字符串(按回车键结束):" << endl; CreatHString(H); cout << "现在串中的字符为:" << endl; PrintHString(H); HStringLength(H); cout << "请再输入一个字符串(按回车键结束):" << endl; CreatHString(T); HStringCompare(H, T); ConcatHString(S, H, T); cout << "现在串中的字符为:" << 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 << "检验S清空后是否为空:" << endl; PrintHString(S); } //main |
原文链接:http://blog.csdn.net/qq_32353771/article/details/49760259