串是由零个或者是多个字符组成的有限序列。
一般记为s=‘a1a2a3…an’ (n>=0)
其中串名称为s,ai(1<=i<=n)可以是字母、数字、或者是其他字符,串中字符数目n称之为串长度,零个字符的串称之为空串。需要注意的是:串值必须要用一对单引号括起来,但是单引号本身不属于串,他的作用是避免与变量名或者是数的常量混合,例如s=123,就说明s变量值为数值123,但是s='123’则表明串变量的内容是字符序列123
具体代码实现为:
#include <iostream>
#include <stdlib.h>
#include <string.h>
#define maxstrlen 60
#define error -1
#define ok 1
using namespace std;
typedef char String[maxstrlen + 1];
int StrAssign(String &T, char *chars)
{
int i;
if (strlen(chars) > maxstrlen)
return error;
else
{
T[0] = strlen(chars) + 1;
for (i = 1; i <= T[0]; i++)
T[i] = *(chars + i - 1);
return ok;
}
}
void StrCopy(String T, String S)
{
int i;
for (i = 0; i <= S[0]; i++)
T[i] = S[i];
}
int StrEmpty(String S)
{
if (S[0] == 0)
return ok;
else
return error;
}
int StrCompare(String S, String T)
{
int i;
for (i = 1; i < S[0] && i < T[0]; ++i)
if (S[i] != T[i])
return S[i] - T[i];
return S[0] - T[0];
}
int StrLength(String S)
{
return S[0];
}
void ClearStr(String S)
{
S[0] = 0;
}
int Concat(String T, String S1, String S2)
{
int i;
if (S1[0] + S2[0] <= maxstrlen)
{
for (i = 1; i < S1[0]; i++)
T[i] = S1[i];
for (i = 1; i < S2[0]; i++)
T[S1[0] + i-1] = S2[i];
T[0] = S1[0] + S2[0];
return ok;
}
else
{
for (i = 1; i < S1[0]; i++)
T[i] = S1[i];
for (i = 1; i <= maxstrlen - S1[0]; i++)
T[S1[0] + i-1] = S2[i];
T[0] = maxstrlen;
return error;
}
}
int SubStr(String Sub, String S, int pos, int len)
{
int i;
if (pos < 1 || pos > S[0] || len < 0 || len > S[0] - pos + 1)
return error;
for (i = 1; i <= len; i++)
Sub[i] = S[pos + i - 1];
Sub[0] = len + 1;
return ok;
}
int Index(String S, String T, int pos)
{
int i, j;
if (1 <= pos && pos <= S[0])
{
i = pos;
j = 1;
while (i <= S[0] && j <= T[0])
{
if (S[i] == T[i])
{
++i;
++j;
}
else
{
i = i - j + 2;
j = 1;
}
if (j > T[0])
return (i - T[0]);
else
return 0;
}
}
else
return 0;
}
int StrInsert(String S, int pos, String T)
{
int i;
if (pos < 1 || pos > S[0] + 1)
return error;
if (S[0] + T[0] <= maxstrlen)
{
for (i = S[0]-1; i >= pos; i--)
S[i + T[0]-1] = S[i];
for (i = pos; i < pos + T[0]-2; i++)
S[i] = T[i - pos + 1];
S[0] += T[0]-1;
return ok;
}
else
{
for (i = maxstrlen; i >= pos + T[0]; i--)
S[i] = S[i - T[0]];
for (i = pos; i <= pos + T[0] && i <= maxstrlen; i++)
S[i] = T[i - pos + 1];
S[0] = maxstrlen;
return error;
}
}
int StrDelete(String S, int pos, int len)
{
int i;
if (pos < 1 || pos > S[0] - len + 1 || len < 0)
return error;
for (i = pos + len; i <= S[0]; i++)
S[i - len] = S[i];
S[0] -= len;
return ok;
}
int Replace(String S, String T, String V)
{
int i = 1, k;
if (StrEmpty(T))
return error;
do
{
i = Index(S, T, i);
if (i)
{
StrDelete(S, i, StrLength(T));
k = StrInsert(S, i, V);
if (!k)
return error;
i += StrLength(V);
}
} while (i);
return ok;
}
void StrPrint(String T)
{
int i;
for (i = 1; i < T[0]; i++)
cout << T[i] << " ";
cout << endl;
}
int main()
{
int i, j, k;
char s, c[maxstrlen];
String T, S1, S2;
cout << "please input string S1:";
gets(c);
k = StrAssign(S1, c);
if (!k)
{
cout << "the length of string over maxstrlen(=" << maxstrlen << ")" << endl;
exit(0);
}
cout << "the length of string is:" << StrLength(S1) << " whether is empty(1:empty -1:not empty):" << StrEmpty(S1) << endl;
StrCopy(S2, S1);
cout << "string copy successfully" << endl;
StrPrint(S2);
cout << "please input string S2:";
gets(c);
k = StrAssign(S2, c);
if (!k)
{
cout << "the length of string over maxstrlen(=" << maxstrlen << ")" << endl;
exit(0);
}
cout << "the length of string is:" << StrLength(S2) << " whether is empty(1:empty -1:not empty):" << StrEmpty(S2) << endl;
StrPrint(S2);
i = StrCompare(S1, S2);
if (i < 0)
s = '<';
else if (i == 0)
s = '=';
else
s = '>';
cout << "string S1 " << s << "string S2" << endl;
k = Concat(T, S1, S2);
cout << "string S1 concat with S2 is:";
StrPrint(T);
if (k == error)
cout << "there is truncation exit in string T" << endl;
ClearStr(S1);
cout << "after clear up string S1:";
StrPrint(S1);
cout << "the length of string is:" << StrLength(S1) << " whether is empty(1:empty -1:not empty):" << StrEmpty(S1) << endl;
cout << "To find a substring, enter the starting position of the substring and the length of the substring:" << endl;
cout << "i=";
cin >> i;
cout << "j=";
cin >> j;
k = SubStr(S2, T, i, j);
if (k)
{
cout << "the substring S2:";
StrPrint(S2);
}
cout << "from the position of pos to delete length of len,please input pos and len:" << endl;
cout << "pos=";
cin >> i;
cout << "len=";
cin >> j;
StrDelete(T, i, j);
cout << "after delete string T:";
StrPrint(T);
i = StrLength(S2) / 2;
StrInsert(S2, i, T);
cout << "after insert string T before the " << i << "th char the string S2" << endl;
StrPrint(S2);
i = Index(S2, T, 1);
cout << "begin in the " << i << " th char in the string S2 to compare with S2" << endl;
SubStr(T, S2, 1, 1);
cout << "string T:";
StrPrint(T);
Concat(S1, T, T);
cout << "striung S1:";
StrPrint(S1);
k = Replace(S2, T, S1);
if (k)
{
cout << "After replacing the non-overlapping string in string S2 with string S1, string S2 is:";
StrPrint(S2);
}
ClearStr(S2);
system("pause");
}
结果为: