• 自己用c语言实现字符串处理库函数以及扩展


    1.实现基本的c语言库函数:

    int myStrlen( const char* str);//根据传入的字符串首地址获取字符串长度;返回值为长度

    int myStrlen(const char* str)

    {

    if (str == NULL)

    return 0;

    int length=0;

    while (*str != '')

    {

    length++;

    str++;

    }

    return length;

    }


    void* myStrcpy(char* des, const char* source);//复制source到des

    void* myStrcpy(char* des, const char* source)
    {
    if (des == NULL || source == NULL)
    return NULL;
    char *desbak = des;
    while (((*des++) = (*source++)) != '');
    *des = '';
    return desbak;
    }


    void * myStrcat(char* des, const char *source);//将source添加到des的末尾

    void * myStrcat(char* des, const char *source)
    {
    if (source == NULL || des == NULL)
    return NULL;
    if (myStrlen(source) == 0)
    return des;
    des = (char*)realloc(des, myStrlen(des) + myStrlen(source) + 1);

    int i = myStrlen(des);
    while (*source != '')
    {
    *(des + i) = *source;
    i++;
    source++;
    }
    *(des + i) = '';
    return des;
    }


    char* myStrrev(const char* str); //字符串逆转

    char* myStrrev(const char* str)
    {
    int i = 0;
    int len = myStrlen(str);
    char* res = (char*)calloc(len+1,sizeof(char));
    *(res + len) = '';
    for (i = len - 1; i >= 0; i--,str++)
    {
    *(res + i) = *str;
    }
    return res;
    }


    int str2int(const char* str);//将整数字符串转化为整数;返回值为结果整数,失败返回-1

    int str2int(const char* str)
    {
    if (str == NULL)
    return -1;
    char* strbak = str;
    int res = 0;
    while (*str != '')
    {
    if (*str<'0' || *str>'9')
    {
    return -1;
    }
    str++;
    }
    str = strbak;
    while (*str != '')
    {
    res *= 10;
    int temp = *str - 48;
    res += temp;
    str++;
    }
    return res;
    }


    char* int2str(int num);//将正整数转化为字符串,返回字符串首地址

    char* int2str(unsigned int num)
    {
    int numbak = num;
    int n=0;//计算int位数
    while (num != 0)
    {
    n++;
    num /= 10;
    }
    char* res = (char*)calloc(n+1,sizeof(char));
    char* resbak=res;
    num = numbak;
    while (num != 0)
    {
    *res = num % 10 + 48;
    res++;
    num /= 10;
    }
    *res = '';
    resbak = myStrrev(resbak);
    return resbak;
    }

    2.根据上面编写的库函数,实现一些扩展的功能:

    typedef struct _myString //定义字符串结构体,包括字符串及其长度

    {

    char *p;

    int length;

    } myString;

    int initString(myString *string, char* str);//初始化字符串

    int initString(myString *string, char* str)
    {
    if (str == NULL)
    return -1;
    int length = myStrlen(str);

    string->p = (char*)calloc(length + 1,sizeof(char));
    myStrcpy(string->p,str);
    string->length = length;
    return 1;
    }


    void * findChar(const myString *string, const char findCh);//找到特定的字符,返回首地址

    void * findChar(const myString *string, char findCh)
    {
    char* s = string->p;
    while (*s != '')
    {
    if (*s== findCh)
    return s;
    s++;
    }
    return NULL;
    }


    void * findString(const myString *string,const char* findStr);//找到字符串,返回首地址,找不到返回null

    void * findString(const myString *string,const char* findStr)
    {
    if (NULL == findStr)
    return NULL;
    if (myStrlen(findStr) > myStrlen(string->p))
    return NULL;

    char* s = string->p;
    char*sp = NULL;

    int i = 0,j=0;
    for (i = 0; i < string->length - 1; i++)
    {
    char* fs = findStr;
    if (*(s + i) == *(fs))
    {
    sp = s + i;
    for (j = 0; j < myStrlen(findStr) - 1; j++)
    {
    sp++;
    fs++;
    if (*sp != *fs)
    break;
    }
    if (j ==myStrlen(findStr) - 1)
    return s + i;
    }
    }
    return NULL;
    }

    int addChar(myString *string, char addCh, char* pos);//pos为null则在string的后面添加,否则在指定位置添加一个字符;返回-1表示操作失败

    int addChar(myString *string, char addCh, char* pos)//pos为NULL表示在字符串后面添加
    {
    if (pos == NULL)
    {
    //string->p = (char *)realloc(string->p, string->length + 2);
    myStrcat(string->p, &addCh);
    string->length += 1;
    return 1;
    }

    if (pos<string->p || pos>string->p + string->length)
    return -1;

    int i = string->length , addLength = pos-string->p ;
    string->p = (char *)realloc(string->p,string->length+2);
    char *s = string->p;
    *(s + i +1) = '';

    for (i = string->length ; i > addLength; i--)
    {
    *(s + i) = *(s+i-1);
    }
    *(s + i) = addCh;
    string->length += 1;
    return 1;
    }


    int addString(myString *string, const char* addStr, char* pos);//pos为null则在string的后面添加,否则在指定位置添加一个字符串;返回-1表示操作失败

    int addString(myString *string, const char* addStr, char* pos)
    {
    if (pos == NULL)
    {
    myStrcat(string->p, addStr);
    string->length += myStrlen(addStr);
    return 1;
    }

    if (pos<string->p || pos>string->p + string->length||addStr==NULL)
    return -1;

    string->length += myStrlen(addStr);

    int i = string->length+myStrlen(addStr), addLength = pos - string->p;
    string->p = (char *)realloc(string->p, string->length +myStrlen(addStr)+1 );
    char *s = string->p;
    *(s + i ) = '';

    for (i = string->length+myStrlen(addStr)-1; i > addLength; i--)
    {
    *(s + i) = *(s + i - myStrlen(addStr));
    }
    while (*addStr != '')
    {
    *(s + i) = *addStr;
    i++;
    addStr++;
    }

    return 1;
    }

    void deleteChar(myString *string,char deleteCh,int tag);//tag为1,则删除所有的指定字符;否则,只删除第一个

    void deleteChar(myString *string, char deleteCh,int tag)
    {
    char * pos = (char*)findChar(string, deleteCh);
    if (pos == NULL)
    return ;

    while (*pos != '')
    {
    *pos = *(pos+ 1);
    pos++;
    }
    string->length -= 1;
    if (tag == 1)
    {
    while (findChar(string, deleteCh) != NULL)
    deleteChar(string, deleteCh,1);
    }
    }


    void deleteString(myString *string,const char *deleteStr,int tag);//tag为1,则删除所有的指定字符;否则,只删除第一个;

    void deleteString(myString *string, const char *deleteStr,int tag)
    {
    char * pos = (char*)findString(string, deleteStr);
    if (pos == NULL)
    return;

    while (*(pos+myStrlen(deleteStr)-1) != '')
    {
    *pos = *(pos + myStrlen(deleteStr));
    pos++;
    }
    string->length -= myStrlen(deleteStr);
    if (tag ==1)
    {
    while (findString(string, deleteStr) != NULL)
    deleteString(string, deleteStr,1);
    }
    }

    void updateChar(myString *string,char oldCh,char newCh);//更新一个字符

    void updateChar(myString *string, char oldCh, char newCh)
    {
    char* pos;
    if ((pos = findChar(string, oldCh)) == NULL)
    return;
    *pos = newCh;
    while (findChar(string, oldCh) != NULL)
    updateChar(string,oldCh,newCh);
    }


    void updateString(myString *string, char* oldStr, const char* newStr);//更新字符串

    void updateString(myString *string, char* oldStr, const char* newStr)
    {
    if (newStr == NULL)
    return;
    char* pos;
    if ((pos = findString(string, oldStr)) == NULL)
    return;
    deleteString(string,oldStr,0);
    addString(string,newStr,pos);
    while (findString(string, oldStr) != NULL)
    {
    updateString(string,oldStr,newStr);
    }
    }

  • 相关阅读:
    分享
    分享
    分享
    分享
    基于加权相似度的相关性排序算法的研究 中国学术期刊网络出版总库
    分享
    基于用户点击行为的数字图书搜索系统研究与实现 中国优秀硕士学位论文全文数据库
    分享
    垂直搜索引擎分类索引系统的设计与实现 中国优秀硕士学位论文全文数据库
    基于相关反馈技术的检索结果排序模型研究 中国优秀硕士学位论文全文数据库
  • 原文地址:https://www.cnblogs.com/HIT-LYT/p/4280906.html
Copyright © 2020-2023  润新知