#include<iostream>
#include<string.h>
using namespace std;
#define maxsize 20
void initstr(struct stringsq* p); //初始化
int strlength(struct stringsq* p); //求字符串的长度
void Createstr(struct stringsq* p); //创建
int emptystr(struct stringsq* p); //判断字符串是否为空
void printfstr(struct stringsq* p); //输出字符串
void strAssign(struct stringsq* p, char temp[]); // 生成一个其值等于字符串chars的串
void strcopy(struct stringsq* p1, struct stringsq* p2); //串p2存在,由串p2复制得串p1
void clearstring(struct stringsq* p); //将串S清空
int strcompare(struct stringsq* p1, struct stringsq* p2); //比较字符串大小
void concat(struct stringsq* t, struct stringsq* p1, struct stringsq* p2); //连接字符串
void insertstring(struct stringsq* p1, int pos, const struct stringsq* p2); //字符串的插入操作
int deletestr(struct stringsq* p, int i, int j) //删除字符串,从p1中删除第i个字符开始的长度为j的子串
struct stringsq* substring(struct stringsq* p, int i, int j) //返回串p中第i个字符起长度为j的子串
int index(struct stringsq* p1, struct stringsq* p2)
//p1 p2均为非空串,若p1中存在子串与p2相等,返回位置。否则,返回0
struct stringsq {
char data[maxsize];
int length;
};
void initstr(struct stringsq* p) //初始化
{
p->length = 0;
}
int strlength(struct stringsq* p) //求字符串的长度
{
int i = 0;
while (p->data[i] != '\0') {
i++;
}
return i;
}
void Createstr(struct stringsq* p) //创建
{
cin>> p->data;
p->length =strlength(p);
}
int emptystr(struct stringsq* p) //判断字符串是否为空
{
if (p->length == 0) {
return 1;
}
else {
return 0;
}
}
void printfstr(struct stringsq* p) //输出字符串
{
cout<< "输出字符串:";
if (emptystr(p)==1) {
cout<< "空字符串!" << endl;
return;
}
for (int i = 0; i < p->length; i++) {
cout<< p->data[i];
}
cout<< endl;
}
void strAssign(struct stringsq* p, char temp[]) //生成一个其值等于字符串chars的串
{
int i;
for (i = 0; temp[i] != '\0'; i++) {
p->data[i] = temp[i];
}
p->length = i;
}
void strcopy(struct stringsq* p1, struct stringsq* p2) //串p2存在,由串p2复制得串p1
{
int i;
for(i=0;i<p2->length;i++){
p1->data[i] = p2->data[i];
}
p1->length = p2->length;
}
void clearstring(struct stringsq* p) //将串S清空
{
int i;
for (i = 0; i < p->length; i++)
{
p->data[i] = '\0';
}
p->length = 0;
}
int strcompare(struct stringsq* p1, struct stringsq* p2) //比较字符串大小
{
int i = 0;
while ((p1->data[i] == p2->data[i])&& p1->data[i] != '\0' && p2>data[i] != '\0')
{
i++;
}
return (p1->data[i] - p2->data[i]);
}
void concat(struct stringsq* t, struct stringsq* p1, struct stringsq* p2) //连接字符串
{
int i,j;
for (i = 0; i < p1->length; i++) {
t->data[i] = p1->data[i];
}
for (j = 0; j < p2->length; j++) {
t->data[i] = p2->data[j];
i++;
}
t->data[j] = '\0';
t->length = p1->length + p2->length;
}
void insertstring(struct stringsq* p1, int pos, const struct stringsq* p2) //字符串的插入操作
{
/*能完全插入且没有元素丢弃
其余情况(不能完全插入1、p2完全插入,p1有部分元素丢弃 2、p2未完全插入,p1与p2都有元素丢弃)*/
int i;
for (i = p1->length - 1; i>= pos - 1; i--) {
p1->data[i + p2->length] = p1->data[i];
}
for (int j = 0; j < p2->length; j++) {
i++;
p1->data[i] = p2->data[j];
}
p1->data[p1->length + p2->length] = '\0';
p1->length = p1->length + p2->length;
}
int deletestr(struct stringsq* p, int i, int j) //删除字符串,从p1中删除第i个字符开始的长度为j的子串
{
if (i < 0 || i>p->length || i - 1 + j>p->length) {
cout<< "删除失败,位置不正确!" << endl;
return 0;
}
else
{
for (int k = i - 1; k < p->length - j; k++) {
p->data[k] = p->data[k + j];
}
p->length = p->length - j;
p->data[p->length] = '\0';
return 1;
}
}
struct stringsq* substring(struct stringsq* p, int i, int j) //返回串p中第i个字符起长度为j的子串
{
if (i<1 || i>p->length || i - 1 + j>p->length) {
cout<< "取子串失败!位置不当!" << endl;
return NULL;
}
struct stringsq* str2 = (struct stringsq*)malloc(sizeof(struct stringsq));
int k;
for (k = 0; k < j; k++)
{
str2->data[k]= p->data[i - 1];
i++;
}
str2->length= j;
str2->data[j] = '\0';
return str2;
}
int index(struct stringsq* p1, struct stringsq* p2)
//p1 p2均为非空串,若p1中存在子串与p2相等,返回位置。否则,返回0
{
int i = 1;
while (i <= p1->length - p2->length + 1)
{
struct stringsq* temp = substring(p1, i, p2->length);
if (strcompare(temp, p2) != 0) //如果两串不相等
{
i++;
}
else {
return i; //相等,返回位置
}
}
return 0; //若无子串与p2相等,返回0
}
数据结构 串的基本操作c++实现
最新推荐文章于 2024-06-24 10:33:09 发布