串及其操作(C语言版)

串(或者是字符串):由一个或者多个字符组成的有限序列,一般标记为 s = ‘abcd…’。其中s称之为串名;串中字符的数目n称为串的长度 ;零个字符的串称为空串;串中由任意个连续的字符组成的子序列称为子串;包含子串的串相应的称为主串;字符在串中的位置称为该字符在串中的位置;子串在主串中的位置以子串第一个字符的位置为准。

串相等:当且仅当串中的长度相等,且各个对应位置的字符都相等时才相等。
空串和空格串:
空串是串的长度为0,串内没有一个元素;例如:“”
空格串是由一个或者是多个或者是多个空格符组成的串;例如:“ ”

串中所定义的操作:

  • StrAssign(&T,chars);初始化串,生成一个值为chars的串
  • StrCopy(&T,S);由串S复制得串T
  • StrEmpty(S);若S为空则返回为true
  • StrCompare(T,S);比较两个字符串,若T**大于S返回1,T等于S返回0,T小于**S返回-1(可以自己定义)
  • Index(S,T,pos);若S中存在和T相等的子串,则返回主串S中第pos个字符之后第一次出现的位置,否则返回0
  • ClearString(&S);将S清为空串
    串的实现(C语言版):
/*串及其相关操作的实现(C语言版)-----串的堆分配存储*/

#include<stdio.h>
#include<malloc.h>
#include<stddef.h>

#define overflow_error 0
#define OK 1
#define operate_error 2
#define queue_null_error 3

typedef struct{
    char *ch;
    int length;
}HString;

/*构造一个值等于chars的字符串T*/
int StrAssign(HString *T,char *chars){
    if((*T).ch){
        free((*T).ch);
    }
    int i;
    char *c;
    for(i = 0,c = chars;*c;++i,++c);
    if(!i){
        (*T).ch = NULL;
        (*T).length = 0;
    }else{
        if(!((*T).ch = (char *)malloc(i*sizeof(char)))){
            exit(overflow_error);
        }
        int j = 0;
        for(j = 0;j<i;j++){
            (*T).ch[j] = chars[j];
        }
        (*T).length = i;

    }
    return OK;
}

/*获取串S的长度*/
int StrLength(HString S){
    return S.length;
}

/*  比较两个字符串的大小,
    S>T则返回值大于0,
    S=T则返回值等于0,
    S<T则返回值小于0
    比较规则是字符在ASCII中对应的的数制*/
int StrCompare(HString S,HString T){
    int i = 0;
    for(i;i<S.length&&i<T.length;i++){
        if(S.ch[i] != T.ch[i]){
            return S.ch[i]-T.ch[i];
        }
    }
    return S.length-T.length;
}

/*显示串*/
void StrShow(HString T,char name){
    int i = 0;
    printf("串%c为\n",name);
    for(i;i<T.length;i++){
        printf("%c",T.ch[i]);
    }
    printf("\n");
}

/*清空串*/
int StrClear(HString *S){
    if((*S).ch){
        free((*S).ch);
        (*S).ch = NULL;
    }
    (*S).length = 0;
    return OK;
}

/*将两个串拼接成一个穿,并用T返回结果*/
int Concat(HString *T,HString S1,HString S2){
    if((*T).ch){
        free((*T).ch);
    }
    //重新申请空间
    if(!((*T).ch = (char *)malloc((S1.length+S2.length)*sizeof(char)))){
        exit(overflow_error);
    }
    //先把S1复制到T中
    int i = 0;
    for(i;i<S1.length;i++){
        (*T).ch[i] = S1.ch[i];
    }
    (*T).length = S1.length+S2.length;
    i = 0;
    for(i;i<(*T).length;i++){
        (*T).ch[S1.length+i] = S2.ch[i];
    }
    return OK;

}

/*用Sub返回串S的第pos个字符起长度为len的子串*/
int SubString(HString *Sub,HString S,int pos,int len){
    //判断pos和len是否合法
    if(pos<1 || pos>S.length || len<0 || len>S.length-pos+1){
        return operate_error;
    }
    if((*Sub).ch){
        free((*Sub).ch);
    }
    if(!len){
        (*Sub).ch = NULL;
        (*Sub).length = 0;
    }else{
        (*Sub).ch = (char *)malloc(len*sizeof(char));
        int i = 0;
        for(i;i<len;i++){
            (*Sub).ch[i] = S.ch[pos-1+i];
        }
        (*Sub).length = len;
    }
    return OK;
}






/*主函数*/
int main(){
    HString T,S,R,M;
    char *chars = "abcdegsdhfjkhs",*chars2 = "abcdefwyeuihidhsfjks";

    //初始化串T和S
    StrAssign(&T,chars);
    StrAssign(&S,chars2);


    StrShow(T,'T');
    StrShow(S,'S');

    //比较两个串的大小
    printf("S和T的比较结果是%d\n",StrCompare(S,T));

    //清空串T
    StrClear(&T);
    StrAssign(&T,chars);
    StrShow(T,'T');

    //合并S,T,并用R返回结果
    Concat(&R,T,S);
    StrShow(R,'R');

    //截取S串的从pos开始长度为len的子串
    SubString(&R,S,2,3);
    StrShow(R,'R');



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值