从小白开始自学数据结构——第八天【串】

串(string)

        定义:由多个或者0个字符组成的有限序列,又名字符串。
              一般记做s = a[1]a[2]a[3].....a[n](n>=0).

        概念:
              空串:
                    0个字符的串(null string),长度为0
                    可以直接用""表示,也可以记做Φ(alt+42677)
              空格串:
                    只包含空格的串,和空串不同,有长度,也可以不止包含一个空格

              子串、主串:
                    串中任意个数的连续字符组成的子序列称为该串的子串
                    包含子串的串为主串  
              串的相等:
                    当串的长度以及他们各个对应位置的字符都相等的时候,才算是相等。

              串的比较大小:
                    给定两个串:s="a[1]a[2]a[3].....a[n]",t="b[1]b[2].....b[m]",
                        当满足下列条件之一时,s<t.
                    1.n<m,且a[i]=b[i](就是前面的字符相同但是字符多的那个大)
                    2.存在某个k<=min(m,n),使得a[i]=b[i](i=1,2....,k-1),a[k]<b[k]
                          即从不相同的地方开始比较不相同的那个字符的ASCII码的大小
                              ASCII码大的大

              串与线性表的比较:
                    1.二者的逻辑结构相似。
                    2.串针对的是字符集,即串中的元素都是字符
                    3.线性表更关注于单个元素的操作(查找、删除)
                    4.串更多的是查找子串,得到指定位置子串,替换子串等操作。

              串的基本操作:
                    1.生成一个串
                    2.复制串
                    3.空判断  
                    4.求串的长度
                    5.两个串的大小比较
                    6.转小(大)写//ASCII码先是大写才是小写,应该是大写加32变小写
                    7.在第pos个位置插入串
                    8.从pos删除长度为len的串
                    9.清空
                    10.链接
# include <stdio.h>
# include <malloc.h>
# include <string.h>
# include <stdlib.h>

struct String 
{
    char * ch;
    int lenth;
};

void init_string (struct String *);                                                  //初始化

bool SteAssign (struct String *, char * );                                           //生成一个值为字符常量chars的串S

void cout_string (struct String *);                                                  //输出(在最后)

bool copy_string (struct String *, struct String *);                                 //复制串

bool empty_string (struct String *);                                                 //空判断这个和求长度一样在堆分配的没什么用

int compary_string (struct String *, struct String *);                               //两个串的大小比较

bool son_string (struct String *, struct String *, int ,int);                        //求子串

bool transform_string (struct String *);                                             //转小(大)写

bool delete_string (struct String *,int pos, int len, struct String *);              //从pos删除长度为len的串,并将删除的返回

void clear_string (struct String *);                                                 //和初始化一个操作 ,要返回就和删除一个操作

bool attach_string  (struct String *, struct String *, struct String *);             //链接

//int Index_string (struct String *, struct String *, int);                          //判断主串中pos之后有没有和一个串相同的子串,有就返回第一个位置,没有就返回0                                                          

int main ()
{
    char * chars = "abcdefg";
    struct String S, T, M, De, New;
    //初始化
    init_string (&S);
    cout_string (&S);

    SteAssign (&S,chars);
    cout_string (&S);

    init_string (&T);
    copy_string (&S, &T);
    cout_string (&S);


    //S>T返回1,S<T返回-1,S=T返回0

    if(compary_string (&S, &T)>0)
        printf ("S>T\n");
    else if (compary_string (&S, &T)<0)
        printf ("S<T\n");
    else if (compary_string (&S, &T)==0)
        printf ("S=T\n");


    init_string (&M);
    son_string (&S,&M,1,3);
    cout_string (&M);

    transform_string(&S);
    cout_string (&S);

    init_string (&De);
    delete_string (&S, 2, 3, &De);
    cout_string (&S);
    cout_string (&De);

    init_string (&New);
    attach_string (&S, &T,&New);
    cout_string (&New);
    return 0;
}

void init_string (struct String * pS)
{
    pS->ch = NULL;
    pS->lenth = 0;

    return;
}

bool SteAssign (struct String * pS,char * chars)
{
    /*
    取chars的长度
    动态分配长度给S
    判断是否分配成功
    挨个将chars赋给S*/
    int i=0,j=0;

    while (chars[i] != '\0')
    {
        i++;
    }
    pS->ch = (char *)malloc(sizeof(char)* i);
    if (pS->ch == NULL)
        return false;
    while (chars[j]!='\0')
    {
        pS->ch[j]=chars[j];
        j++;
    }
    pS->lenth = j;
    return true;
}

bool copy_string (struct String * pS, struct String * pT)
{
    /*动态分配内存给T
    判断分配成功
    已知长度
    循环
    返回*/
    pT->ch = (char *)malloc (sizeof(char)*pS->lenth );
    if (pT->ch == NULL)
    {
        return false;
    }

    for (int i=0; i<pS->lenth ;i++)
        pT->ch [i]=pS->ch [i];
    pT->lenth = pS->lenth;
    return true;
}


bool empty_string (struct String * pS)
{
    if (pS->ch == NULL)//最好不要是pS->lenth == 0;
        return true;
    else
        return false;
}

//S>T返回正,S<T返回负,S=T返回0
int compary_string (struct String * pS, struct String * pT)
{ 
    int i;
    for (i=0; i<pS->lenth && i<pT->lenth ; i++)
        if (pS->ch[i] != pT->ch[i])
            return pS->ch[i]-pT->ch[i];
        return pS->lenth - pT->lenth;

    /*我想这一坨想了三个小时。。。还是放弃了,人有些时候,死心眼真的不得行
    int j=0;
    if (pS->lenth > pT->lenth )
        return 1;
    else if (pS->lenth < pT->lenth )
        return -1;
    else if ((pS->lenth == pT->lenth )&& pS->ch==NULL)
    return 2;
    else if ((pS->lenth == pT->lenth )&& pS->ch!=NULL) 
    {
        for (int i=0; i<=pS->lenth; i++)
        {
            if (pS->ch [i]==pT->ch [i]&& pS->ch[i]!='\0')
            {
                printf ("caonima\n");

            }
            else if (pS->ch [i]>pT->ch [i])
            return 1;
            else if (pS->ch [i]<pT->ch [i])
                return -1;
            else if (pS->ch [i]==pT->ch [i]&& pS->ch[i]=='\0')
                return 2;
        }printf ("长度%d",pS->lenth );
     }

    }
    {

        for (int i=0;i<=pS->lenth;)
        {
            if (pS->ch [i]>pT->ch [i])
            {
                return 1;
                break;
            }
            else if (pS->ch [i]<pT->ch [i])
            {
                return -1;
                break;
            }
            else if (pS->ch [i]==pT->ch [i] && pS->ch[i]!='\0')
                i++;
            else if (pS->ch [i]==pT->ch [i] && pS->ch[i]=='\0')
            {
                return 2;
                break;}




            if(pS->ch[i]=='\0')
                return 2;
            else if(pS->ch[j]==pT->ch[j] && pS->ch[j]!='\0')
                j++;
            else if(pS->ch[j]==pT->ch[j] && pS->ch[j]=='\0')
                return 2;
            else if(pS->ch[j]>pT->ch[j])
                return 1;
            else
                return -1;
        }*/

}

bool son_string (struct String * pM, struct String * pS, int pos, int len)
{
    pos=pos-1;
    pS->ch = (char *)malloc(sizeof(char)* len);
    if ( empty_string(pS)==true)
    {
        return false;
    }
    if (pos+len>pM->lenth )
    {
        printf ("长度超出主串范围。");//我也不知道是不是叫母串//叫主串
        return false;
    }
    for (int i=0; i<=len;i++)
    {
        pS->ch[i]=pM->ch [pos];
        pos++;
    }
    pS->lenth = len;
    return true;

}

bool transform_string (struct String * pS)
{
    /*空判断
    遍历判断是否有大(小)写字母
    cuo:大写减32变小写
    小写加32变大写*///ASCII码先是大写才是小写,应该是大写加32变小写
    if ( empty_string(pS)==true)
    {
        printf ("kongde \n");
        return false; 
    }

    for (int i=0; i<pS->lenth ;i++)
    {
        if (pS->ch[i]>='a' && pS->ch[i]<='z')
            pS->ch[i]-=32;
        else //if (pS->ch[i]<'a' && pS->ch[i]>'z')
            continue;
    }
    return true;
}

bool delete_string (struct String * pS,int pos, int len, struct String * pDe)//delete_string (&S,2,3);
{
    pDe->ch = (char *)malloc (sizeof(char)* len);
    pDe->lenth = len;
    if ( empty_string(pS)==true)
    {
        return false;
    }
    if (pos+len>pS->lenth )
    {
        printf ("长度超出被删除串范围。");
        return false;
    }

    for (int j=0;j<len;j++)
    {
        pDe->ch[j]=pS->ch [(pos-1)];//pS->ch [(pos-1)];随着下面的for循环在不断变化
        for (int i=pos; i<pS->lenth ;i++)
        {
            pS->ch[i-1]=pS->ch[i];//pS->ch[pos-1]=pS->ch[pos];是错误的,debug到怀疑人生
        }
        pS->lenth--;
    }
    //pS->lenth = pS->lenth - len;
    return true;
}

void clear_string (struct String * pS)
{
    pS->ch = NULL;
    pS->lenth = 0;

    return;
}

bool attach_string  (struct String * pS, struct String * pT, struct String * pNew)
{
    int j=0;
    pNew->lenth = pT->lenth + pS->lenth ;
    pNew->ch = (char*)malloc (sizeof(char)*(pNew->lenth));
    if ( empty_string(pNew)==true)
    {
        return false;
    }
    for (int i=0; i<pS->lenth;i++)
        pNew->ch[i]=pS->ch[i];

    for (i=pS->lenth; i<pNew->lenth; i++,j++)
        pNew->ch[i]=pT->ch[j];
    return true;
}
void cout_string (struct String * pS)
{
    if ( empty_string(pS)==true )
    {
        printf ("字符串为空\n");
        return;
    }
    else
        for (int i=0; i<pS->lenth; i++)
            printf ("%c", pS->ch [i]);
    printf ("\n字符长度为:%d\n", pS->lenth );

    return;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值