【数据结构】4.1串的基本操作(附代码实现)

串的基本操作有:

  1. 赋值
  2. 连接
  3. 比较
  4. 清空
  5. 求子串

具体代码如下

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

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

void StrInit(Str &str);//有多个由结构体创造出来的变量时(str1,str2,str3),不初始化很危险! 
int StrAssign(Str &str ,char * ch) ;//赋值 
int GetStrLength(Str str);//获取长度 
int StrCompare(Str s1,Str s2);//比较 
int Concat(Str str1, Str str2, Str &str);//连接 
int GetSubString(Str str,int pos, int len, Str &substr);//求子串 
int ClearString(Str &str); //清空 

int main(void)
{
    Str str1, str2, str3,substr;
    StrInit(str1);
    StrInit(str2);
    StrInit(str3);
    StrInit(substr);

    char ch1[] = "I love China!";
    char ch2[] = "I love U!";
    char ch3[] = "Data structure!";

    StrAssign(str1, ch1);
    StrAssign(str2, ch2);
    StrAssign(str3, ch3);
    Concat(str1,str2,str3) ;
    GetSubString(str3,3,7,substr);
    printf("%s\n",str1.ch);
    printf("%s\n",str2.ch);
    printf("%s\n",str3.ch);
    printf("%s\n",substr.ch);
    return 0;
}

void StrInit(Str &str)
{   
    str.length = 0;
    str.ch = NULL;
}

int StrAssign( Str & str ,char * ch)
{   //赋值 
    if(str.ch)          //如果str.ch原先有存其他东西,就释放掉原先的东西 
    {
        free(str.ch);
        str.ch = NULL;
    }   
    int len = 0;
    char * c = ch;
    while (*c) 
    {  //求串长 
        ++len;
        ++c;
    } 
    if(len == 0)    //空串 
    {
        str.ch = NULL;
        str.length = 0;
        return 1; 
    }
    else
    {
        str.ch = (char *)malloc(sizeof(char)*(len + 1));
        if(str.ch ==NULL)//空间分配失败
            return 0;
        else
        {//写法1 
            c = ch;
            for(int i = 0; i <= len ; ++i,++c)
                str.ch[i] = *c;
            str.length = len;
            return 1;
        } 
//      {//写法2  
//          i = 0;
//          c = ch;
//          while(c*)
//          {
//              str.ch[i] = c*;
//              ++c;
//              ++i;
//          }
//          str.[++i] = '\0';
//          str.length = i;
//      } 
    }
}

int GetStrLength(Str str)
{
    return str.length;
}

int StrCompare(Str str1,Str str2)
{   //串比较 
    for(int i = 0; i <str1.length && i < str2.length; ++i)
        if(str1.ch[i] != str2.ch[i])
            return str1.ch[i] - str2.ch[i];
    return str1.length - str2.length;
}

int Concat(Str str1, Str str2, Str &str)
{   //串连接
    if(str.ch)
    {
        free(str.ch);
        str.ch = NULL;
    }   
    str.ch = (char *)malloc(sizeof(char)*(str1.length + str2.length + 1));
    if(!str.ch)     //空间分配失败 
        return 0;
    int  i = 0,j = 0;
    while (i <str1.length)
    {
        str.ch[i] = str1.ch[i];
        ++i;
    }
    while(j < str2.length)
    {
        str.ch[i + j] = str2.ch[j];
        ++j;
    }
    str.ch[i+j] = '\0';
    str.length = str1.length + str2.length;
    return 1;
}

int GetSubString(Str str,int pos, int len, Str &substr)
{   //求子串:从主串str里的第pos位置后开始,依次把len个元素作为子串substr 
    if(pos < 0 || pos >=str.length || len < 0 || len > str.length - pos)
        return 0;
    if(substr.ch) 
    {
        free(substr.ch);
        substr.ch == NULL;
    }
    if(len == 0)
    {
        substr.ch = NULL;
        substr.length = 0;
        return 1;
    }
    else
    {
        substr.ch = (char*)malloc(sizeof(char) * (len + 1));
        int i = pos, j = 0;
        while(i < pos + len)
        {
            substr.ch[j] = str.ch[i];
            ++i;
            ++j;
        }
        substr.ch[j] = '\0';
        substr.length = len;
        return 1;
    }
}

int ClearString(Str & str)
{   //清空串 
    if(str.ch)
    {
        free(str.ch);
        str.ch == NULL;
    }
    str.length = 0;
    return 1;
}
发布了14 篇原创文章 · 获赞 4 · 访问量 7539

猜你喜欢

转载自blog.csdn.net/qq_39745474/article/details/81811660
今日推荐