数据结构 通俗易懂版 c语言描述----串及其基本操作

2022.2.11

串及其基本操作

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <malloc.h>


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

//赋值操作 ch的值赋给str->ch  eg  Strassign(str,"aaaa");
bool Strassign(Str *str, char *ch)
{

    int i = 0, j = 0;
    char *c = ch;
    while (*c)
    {
        i++;
        c++;
    }

    if (i == 0)
    { // ch为空串 就直接返回空串
        str->ch = '\0';
        str->length = 0;
        return true;
    }
    else
    {
        str->ch = (char *)malloc(sizeof(char) * (i + 1));
        if (str->ch == NULL)
            return false;
        c = ch;
        for (j; j < i; j++)
        {
            str->ch[j] = *c;
            c++;
        }
        str->ch[j] = '\0';
        str->length = i;
        return true;
    }
}

//比较操作
int StrCompare(Str *s1, Str *s2)
{
    int i = s1->length, j = s2->length;

    for (int a = 0; a < i && a < j; a++)
    {
        if (s1->ch[a] != s2->ch[a])
        {
            return (s1->ch[a]) - (s2->ch[a]);
        }
    }
    // for循环结束 则说明其中一个串已经完了且两个串前面全部相同 此时比串的长度就行
    return (s1->length) - (s2->length);
}

//连接俩个串
bool concat(Str *s, Str *s1, Str *s2)
{

    s->ch = (char *)malloc(sizeof(char) * (s1->length + s2->length + 1));
    if (s->ch == NULL)
        return false;

    char *c1 = s1->ch;
    char *c2 = s2->ch;
    int i = 0;

    for (i; i < s1->length; i++)
    {
        s->ch[i] = *c1;
        c1++;
    }

    for (int j = 0; j < s2->length + 1; j++)
    {
        s->ch[i++] = *c2;
        c2++;
    }

    s->length = s1->length + s2->length;
    return true;
}

//求子串操作   pos为起始位置   len为子串的总长度
bool Substring(Str *s, Str *r, int pos, int len)
{
    if (pos < 0 || pos > r->length || len < 0 || pos + len > r->length)
        return false;
    s->ch = (char *)malloc(sizeof(char) * (len + 1));
    int j = 0;
    if (len == 0)
    {
        s->ch = '\0';
        s->length = 0;
        return true;
    }
    else
    {
        for (int i = pos; i < pos + len; i++, j++)
        {
            s->ch[j] = r->ch[i];
        }
        s->ch[j] = '\0';
        s->length = len;
        return true;
    }
}
//测试
int main()
{
    Str *p;
    p = (Str *)malloc(sizeof(Str));
    Strassign(p, "mmmm ???m,m-.-\n");
    printf("%s-%d", p->ch, p->length);

    Str *s, *s1, *s2, *s3;
    s = (Str *)malloc(sizeof(Str));
    s1 = (Str *)malloc(sizeof(Str));
    s2 = (Str *)malloc(sizeof(Str));
    s3 = (Str *)malloc(sizeof(Str));
    s1->ch = "abaa ac ", s1->length = 8;
    s2->ch = "aaa bbb ccc", s2->length = 11;

    int a = StrCompare(s1, s2);
    printf("%d\n", a);

    concat(s, s1, s2);
    printf("%s-%d\n", s->ch, s->length);

    Substring(s3, s, 2, 5);
    printf("%s\n", s3->ch);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值