#include<iostream>
using namespace std;
#define maxSize 256
typedef struct {
char ch[maxSize + 1];
int len;
}SeqString;
void CreateSeqString(SeqString&S, char ch[])
{
int i = 0;
while (i < maxSize&&ch[i] != '\0')
{
S.ch[i] = ch[i];
i++;
}
if (ch[i] != '\0')
{
cout << "字符串过大";
S.len = 0;
}
else
{
S.len = i;
S.ch[i] = '\0';
}
}
void PrintSeqString(SeqString&S)
{
for (int i = 0;i < S.len;i++)
cout << S.ch[i];
}
void ConCat(SeqString&S1, SeqString&S2)
{
if (S1.len + S2.len <= maxSize)
{
int i = S1.len;
for (;i < S1.len + S2.len;i++)
{
S1.ch[i] = S2.ch[i - S1.len];
}
S1.ch[i] = '\0';
S1.len += S2.len;
}
else cout << "存储空间不足";
}
int main()
{
SeqString S1;
SeqString S2;
char C[] = "string";
CreateSeqString(S1, C);
CreateSeqString(S2, C);
ConCat(S1, S2);
PrintSeqString(S1);
system("pause");
}
C++自定义字符串的存储表示
最新推荐文章于 2021-07-10 12:22:19 发布