串的定长顺序存储C语言实现

串(字符串)是由0个或多个字符组成的有限序列。0个字符时称为空串。由一个或多个空格组成的串‘ ’称为空格串。串中字符的数目n称为串的长度;串中任意个连续的字符组成的子序列称为该串的字串;包含字串的串相应的称为主串;通常称字符在序列中的序号称为该字符在串中的位置。字串在主串中的位置则以字串的第一个字符在主串中的位置来表示。串相等:只有两个串的长度相等,并且各个对应位置的字符都相等时才相等。

串的操作中,通常是以“串的整体”作为操作对象的。基本操作如下(红色标注为串的最小操作子集):

// chars是字符串常量,生成一个其值等于chars的串T

StrAssign(&T, chars)


// 串S存在,由串S复制得串T

StrCopy(&T, S)


// 串S存在,若串S为空,则返回TRUE,否则返回FALSE

StrEmpty(S)


// 串S和T存在,若S > T,则返回值 > 0;若S = T,则返回值 = 0;若S < T,则返回值 < 0

StrCompare(S, T)


// 串S存在,返回S的元素个数,称为串的长度

StrLength(S)


// 串S存在,将S清为空串

ClearString(&S)


// 串S1和S2存在,用T返回由S1和S2联接而成的新串

Concat(&T, S1, S2)


// 串S存在,1 <= pos <= StrLength(S) 且 0 <= len <= StrLength(S) - pos + 1,用Sub返回串S的第pos个字符起长度为len的字串

SubString(&Sub, S, pos, len)


// 串S和T存在,T是非空串,1 <= pos <= StrLength(S) ,若主串S中存在和串T值相同的字串,则返回它的主串S中第pos个字符之后第一次出现的位置;否则函数值为0

Inxdex(S, T, pos)


// 串S,T和V存在,T是非空串;用V替换主串S中出现的所有与T相等的不重叠的字串

Replace(&S, T, V)


// 串S和T存在, 1 <= pos <= StrLength(S) + 1;在串S的第pos个字符之前插入串T。

StrInsert(&S, pos, T)


// 串S存在, 1 <= pos <= StrLength(S) - len +1 ;从串S中删除第pos个字符起长度为len的字串

StrDelete(&s, pos, len)


// 串S存在, 销毁串S

DestroyString(&S)


#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      

#define TRUE 1
#define FALSE 0
#define MAXSTRLEN	255

unsigned char SString[MAXSTRLEN + 1];
typedef int Status;

Status StrAssign(unsigned char T[], char *chars);
Status StrLength(unsigned char S[]);
Status StrCopy(unsigned char T[], unsigned char S[]);
Status StrEmpty(unsigned char S[]);
Status StrCompare(unsigned char T[], unsigned char S[]);
Status Concat(unsigned char T[], unsigned char S1[], unsigned char S2[]);
Status SubString(unsigned char Sub[], unsigned char S[], int pos, int len);
Status Index(unsigned char S[], unsigned char T[], int pos);
//Status ClearString(&S);
//Status Replace(&S, T, V);
//Status StrInsert(&S, pos, T);
//Status StrDelete(&S, pos, len);
//Status DestroyString(&S);

/*
 * @:T : the string you want to creat
 * @:char *: the string
 * return TRUE(success), FALSE(failure)
 */
Status StrAssign(unsigned char T[], char *chars)
{
	int i = 0;
	if(strlen(chars) > MAXSTRLEN){
		return FALSE;	
	}else{
		T[0] = strlen(chars);
		for(i = 1; i <= T[0]; i++){
			T[i] = *(chars + i - 1);	
		}	
		return TRUE;
	}
}

/*
 *@S:the length you want to test
 *return the length of S
 */
Status StrLength(unsigned char S[])
{
	return S[0];
}

/*
 *@S:the string you want to test
 *return TRUE:the string is empty
 *       FALSE:the string is not empty
 */
Status StrEmpty(unsigned char S[])
{
	return S[0] ? FALSE : TRUE;
}
/*
 * @T: the string you want to copy to 
 * @S: the string you want to copy
 */
Status StrCopy(unsigned char T[], unsigned char S[])
{
	int i = 0;
	while(S[i] != '\0')
			T[i] = S[i++];
}

/*
 *@T S : the strings you want to compare
 *return 0:T=S
 *      <0:T
      
      0:T>S
 */
Status StrCompare(unsigned char T[], unsigned char SString[])
{
	int i = 0;
	
	for(i = 1; i < T[0] && i < SString[0]; ++i){
		if(T[i] != SString[i]){
			return (T[i] - SString[i]);	
		}	
	}
	return (SString[0] - T[0]);
}

/*
 *
 */
Status Concat(unsigned char T[], unsigned char S1[], unsigned char S2[])
{
	int i = 0;
	int j = 0;

	if(S1[0] + S2[0] <= MAXSTRLEN){
		for(i = 1; i <= S1[0]; i++){
			T[i] = S1[0];	
		}	
		for(i = 1; i<= S2[0]; i++){
			T[S1[0] + i] = S2[i];	
		}
		return TRUE;
	}else{
		T[0] = MAXSTRLEN;
		for(i = 1; i <= S1[0]; i++){
			T[i] = S1[i];	
		}	
		for(i = 1; i <= MAXSTRLEN-S1[0]; i++){
			T[S1[0] + i] = S2[i];	
		}
		return FALSE;
	}	
}

Status SubString(unsigned char Sub[], unsigned char S[], int pos, int len)
{
	int i;
	if(pos < 1 || pos > S[0] || len < 0 || len > S[0]-pos+1)
		return FALSE;
	
	for(i = 1; i < len; i++)
	{
		Sub[i] = S[pos+i-1];	
	}
	Sub[0] = len;
	return TRUE;
}

Status Index(unsigned char S[], unsigned char T[], int pos)
{
	int i, n, m;
	if(pos > 0){
		n = StrLength(S);	
		m = StrLength(T);	
		i = pos;

		while(i <= n-m+1){
			if(StrCompare(S, T) != 0)	i++;
			else return i;
		}
	}
	return TRUE;
}

int
main(void)
{
	char *chars = "fakfjaldkf";
	unsigned char T[1024];
	if(StrAssign(SString, chars))
		printf("strassign success.\n");
	else
		printf("strassign failure.\n");

	printf("the length of string is : %d.\n", StrLength(SString));

	printf("the string of is %s.\n", (StrEmpty(SString) ? "empty" : "not empty"));

	StrCopy(T, SString);
	printf("%d\n",StrCompare(T, SString));

	Concat(T, T, SString); 

	SubString(T, T, 2, 2);
	printf("the length of string is : %d.\n", StrLength(T));

	Index(SString, T, 0);
}

     
     
    
    
   
   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值