顺序串的练习

顺序串的练习

#include<iostream>
#include<stdio.h>
#define MAXSIZE 1000
//顺序串的结构体
typedef struct string							
{
	char ch[MAXSIZE + 1];
	int length;
}string;


int StringAssign(string* S, char chs[]) {
	int i = 0;
	while (chs[i] != '\0') {
		S->ch[i] = chs[i];
		i++;
	}
	S->length = i;
	return 0;
}

void StringCopy(string* S1, string* S2) {
	int i = 0;
	while (S2->ch[i] != '\0') {
		S1->ch[i] = S2->ch[i];
		i++;
	}
	S1->length = S2->length;

}

int LengthString(string* S) {
	return S->length;
}

int ShowString(string* S) {
	if (S->length <= 0)
		return 0;
	else {
		for (int i = 0; i < S->length; i++) {
			printf("%c", S->ch[i]);
		}
		printf("\n");
	}
}


int StringCompare(string* S1, string* S2) {
	int i = 0;
	while (S1->ch[i] != '\0' && S2->ch[i] != '\0') {
		if ((int)(S1->ch[i]) > (int)(S2->ch[i]))
			return 1;
		else if ((int)(S1->ch[i]) < (int)(S2->ch[i]))
			return -1;
		else {
			i++;
		}

	}
	if (S1->ch[i] == '\0' && S2->ch[i] != '\0')
		return -1;
	else if (S1->ch[i] != '\0' && S2->ch[i] == '\0')
		return 1;
	else
		return 0;

}


int ConcatString(string* S, string* S1, string* S2) {
	int i = 0, j = 0;
	for (; i < S1->length; i++) {
		S->ch[i] = S1->ch[i];
	}
	for (; j < S2->length; j++, i++)
		S->ch[i] = S2->ch[j];
	S->length = i;

}

int SubString(string* Sub, string* S, int pos, int len) {
	if (pos <= 0 || len >= S->length || len <= 0 || pos > S->length - len + 1) {
		printf("输入错误!!!");
		return 0;
	}
	else {
		
		int i = 0;
		for (; i < len; i++,pos++) {
			Sub->ch[i] = S->ch[pos];

		}

		Sub->length = i;
		return 1;
	}


}


int StrDelete(string* S, int pos, int len) {
	if (len < 1 || pos < S->length + len - 1)
	{
		printf("输出错误!!!");
		return 0;
	}
	else{
		for (int i = pos + len; i < S->length;i++) {
			S->ch[i - len] = S->ch[i];

		}
		S->length -= len;
		return 1;
	}


}


int Index(string* S, string* T, int pos) {
	if (pos<1 || pos>S->length - 1)
	{
		printf("出现错误!!!");
		return 0;
	}
	else {
		int j = 0;
		for (int i = pos; i < S->length-1; i++,j++) {
			T->ch[j] = S->ch[i];

		}
		T->length = j;


	}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值