在本篇博客中,定义的字符串为索引1为起始下标。
1.串的定义
#define MAXLEN 255 //预定义最大串长255
typedef struct {
char ch[MAXLEN]; //每个分量存储一个字符
int length; //串的实际长度
}SString;
2. 赋值操作
bool StrAssign(SString &T)
{
//将字符串charts存放到定义的字符串中
char arr[10] = "charts";
int lenArr = strlen(arr);
int i = 0;
for (i = 0; i < lenArr; ++i)
{
T.ch[i + 1] = arr[i];
T.length++;
}
T.ch[i+1] = 0;
return true;
}
3.字符串复制操作
bool StrCopy(SString S)
{
char arr[10] = { 0 };
//将字符串S中的值复制到arr中
int i = 0;
while (arr[i] = S.ch[i + 1])
{
i++;
}
//打印arr查看复制的结果
int lenArr = strlen(arr);
cout << "复制字符串arr的结果:";
for (int i = 0; i < lenArr; ++i)
{
cout << arr[i];
}
cout << endl;
return true;
}
4.判断空
bool StrEmpty(SString S)
{
if (S.length == 0)
return true;
return false;
}
5.比较操作
int StrCompare(SString S)
{
char T[10] = "chaabc";
//若S>T,返回值1
//若S==T,返回值0
//若S<T,返回值-1
int i = 0;
while (T[i] == S.ch[i + 1])
{
i++;
}
if (S.ch[i + 1] > T[i])
return 1;
else if (S.ch[i + 1] == T[i])
return 0;
else
return -1;
}
6.求串的长度
int StrLength(SString S)
{
return S.length;
}
7.打印全部元素
void PrintSString(SString S)
{
for (int i = 1; i <= S.length; ++i)
{
cout << S.ch[i];
}
cout << endl;
}
8.求字串
int SubString(int &Sub,SString S)
{
char pos[] = "ar";
int lenPos = strlen(pos);
int i = 1, j = 0;
while (i <= S.length && j < lenPos)
{
if (S.ch[i] == pos[j])
{
++i;
++j; //继续比较后续字符串
}
else {
i = i - j + 1;
j = 0;
}
}
if (j >= lenPos)
{
Sub = i - lenPos;
}
else
{
Sub = -1;
}
return Sub;
}
9.串拼接
void Concat(char s1[], char s2[])
{
int i = 0, j = 0;;
char T[30] = { 0 };
while (T[i] = s1[i])
{
i++;
}
while (T[i] = s2[j])
{
i++,j++;
}
cout << "拼接后的字符出:";
printf("%s", T);
cout << endl;
}
10.清空操作
bool ClearString(SString S)
{
S.length = 0;
return true;
}
11.源代码
#include<iostream>
using namespace std;
#define MAXLEN 255 //预定义最大串长255
typedef struct {
char ch[MAXLEN]; //每个分量存储一个字符
int length; //串的实际长度
}SString;
//赋值操作
bool StrAssign(SString &T)
{
//将字符串charts存放到定义的字符串中
char arr[10] = "charts";
int lenArr = strlen(arr);
int i = 0;
for (i = 0; i < lenArr; ++i)
{
T.ch[i + 1] = arr[i];
T.length++;
}
T.ch[i+1] = 0;
return true;
}
//字符串复制操作
bool StrCopy(SString S)
{
char arr[10] = { 0 };
//将字符串S中的值复制到arr中
int i = 0;
while (arr[i] = S.ch[i + 1])
{
i++;
}
//打印arr查看复制的结果
int lenArr = strlen(arr);
cout << "复制字符串arr的结果:";
for (int i = 0; i < lenArr; ++i)
{
cout << arr[i];
}
cout << endl;
return true;
}
//判断空
bool StrEmpty(SString S)
{
if (S.length == 0)
return true;
return false;
}
//比较操作
int StrCompare(SString S)
{
char T[10] = "chaabc";
//若S>T,返回值1
//若S==T,返回值0
//若S<T,返回值-1
int i = 0;
while (T[i] == S.ch[i + 1])
{
i++;
}
if (S.ch[i + 1] > T[i])
return 1;
else if (S.ch[i + 1] == T[i])
return 0;
else
return -1;
}
//求串的长度
int StrLength(SString S)
{
return S.length;
}
//打印全部元素
void PrintSString(SString S)
{
for (int i = 1; i <= S.length; ++i)
{
cout << S.ch[i];
}
cout << endl;
}
//求字串
int SubString(int &Sub,SString S)
{
char pos[] = "ar";
int lenPos = strlen(pos);
int i = 1, j = 0;
while (i <= S.length && j < lenPos)
{
if (S.ch[i] == pos[j])
{
++i;
++j; //继续比较后续字符串
}
else {
i = i - j + 1;
j = 0;
}
}
if (j >= lenPos)
{
Sub = i - lenPos;
}
else
{
Sub = -1;
}
return Sub;
}
//串拼接
void Concat(char s1[], char s2[])
{
int i = 0, j = 0;;
char T[30] = { 0 };
while (T[i] = s1[i])
{
i++;
}
while (T[i] = s2[j])
{
i++,j++;
}
cout << "拼接后的字符出:";
printf("%s", T);
cout << endl;
}
//清空操作
bool ClearString(SString S)
{
S.length = 0;
return true;
}
int main()
{
SString S;
S.length = 0;
//字符串的第一个位置存放字符串的长度
S.ch[0] = S.length;
//赋值操作
StrAssign(S);
cout << "字符串为:";
PrintSString(S);
cout << "字符串长度:" << S.length << endl;
//字符串复制操作
StrCopy(S);
//判断空
cout<<"字符串" << (StrEmpty(S)?"不为" : "为") << "空"<<endl;
//比较操作
int tmp = StrCompare(S);
cout << "比较操作结果:" << tmp << endl;
//求串的长度
int len = StrLength(S);
cout << "字符串长度:" << len << endl;
//求字串
int Sub = 0;
SubString(Sub, S);
cout << "字串索引为:" << Sub<<endl;
//串拼接
char s1[15] = "Hello ";
char s2[15] = "World!";
Concat(s1, s2);
//清空操作
ClearString(S);
return 0;
}
看到这里了,点一个关注吧!