目前共有6篇帖子。 內容轉換:不轉換▼
 
點擊 回復
425 5
【原创函数】C语言字符串的连接,插入和替换函数
一派護法 十九級
1樓 發表于:2015-12-19 19:09

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

char *str_create(const char *str)
{
    int len = strlen(str);
    char *pStr = (char *)calloc(len + 1, sizeof(char));
    if (pStr != NULL)
        strcpy(pStr, str);
    return pStr;
}

int str_concat(char **p, const char *str)
{
    int oldlen = strlen(*p);
    int len = strlen(str);
    int newsize = oldlen + len + 1;
    char *q = realloc(*p, newsize * sizeof(char));
    if (q == NULL)
        return 0;
    *p = q;
    strcpy(*p + oldlen, str);
    return 1;
}

int str_insert(char **p, const int pos, const char *str)
{
    int oldlen = strlen(*p);
    int len = strlen(str);
    int newsize = oldlen + len + 1;
    char *q = realloc(*p, newsize * sizeof(char));
    if (q == NULL)
        return 0;
    *p = q;
    memmove(*p + pos + len, *p + pos, oldlen - pos + 1);
    memcpy(*p + pos, str, len);
    return 1;
}

int str_replace_once(const char *find, const char *replace, char **p)
{
    char *ch = strstr(*p, find);
    int newsize, oldlen, flen, rlen, pos;
    char *q;
    if (ch == NULL)
        return 0;
    
    pos = ch - *p;
    oldlen = strlen(*p);
    flen = strlen(find);
    rlen = strlen(replace);
    newsize = oldlen - flen + rlen + 1;
    q = realloc(*p, newsize * sizeof(char));
    if (q == NULL)
        return 0;
    *p = q;
    ch = *p + pos;
    memmove(ch + rlen, ch + flen, oldlen - pos - flen + 1); // including \0 at the end
    memcpy(ch, replace, rlen);
    return 1;
}

int str_replace(char *find, char *replace, char **p)
{
    int count = 0;
    while (str_replace_once(find, replace, p))
        count++;
    return count;
}

int str_reset(char **p, const char *newstr)
{
    int len = strlen(newstr);
    char *q = realloc(*p, (len + 1) * sizeof(char));
    if (q == NULL)
        return 0;
    *p = q;
    strcpy(*p, newstr);
    return 1;
}

int main(int argc, char *argv[])
{
    char *str = str_create("abcdef"); // str = "abcdef"
    int count;
    
    puts(str);
    str_concat(&str, "123456"); // str += "123456"
    puts(str);
    str_insert(&str, 2, "CD");
    puts(str);
    str_replace_once("f1", "{mine}", &str);
    puts(str);
    
    str_reset(&str, "ababababca"); // str = "ababababc"
    puts(str);
    count = str_replace("b", "?", &str);
    puts(str);
    printf("Replacement count: %d\n", count);
    count = str_replace("a", "{you}", &str);
    puts(str);
    printf("Replacement count: %d\n", count);
    
    str_insert(&str, 0, "The result is "); // str = str + ...
    str_concat(&str, ".");
    puts(str);
    
    free(str);
    return 0;
}
一派護法 十九級
2樓 發表于:2015-12-19 19:10

【运行结果】
abcdef
abcdef123456
abCDcdef123456
abCDcde{mine}23456
ababababca
a?a?a?a?ca
Replacement count: 4
{you}?{you}?{you}?{you}?c{you}
Replacement count: 5
The result is {you}?{you}?{you}?{you}?c{you}.
一派護法 十九級
3樓 發表于:2015-12-19 19:18

【函数列表】
char *str_create(const char *str)
在堆内存中根据str的长度动态申请一块内存并存储该字符串。失败则返回NULL。

int str_concat(char **p, const char *str)
将字符串p的空间延长后,在尾部与字符串str连接。成功时返回1,失败时返回0。

int str_insert(char **p, const int pos, const char *str)
在指定位置pos插入字符串str,并将后续内容往后挪动。成功时返回1,失败时返回0。

int str_replace_once(const char *find, const char *replace, char **p)
把字符串p中的find字符串替换为replace字符串,只替换一次。成功时返回1,失败时返回0。

int str_replace(char *find, char *replace, char **p)
把字符串p中的所有find字符串全部替换为replace字符串。返回成功替换的个数。注意replace字符串里面不要包含find字符串,否则该函数会无法结束而陷入死循环。

int str_reset(char **p, const char *newstr)
将字符串p的内容重新赋值,并自动调整所占内存大小。
一派護法 十九級
4樓 發表于:2015-12-19 19:20

【说明】
由于C++语言中已经有了很强大的string类,所以电脑上可能并不会用到这些函数。不过这些函数可以在单片机中使用,因为在单片机C语言程序中包含一个字符串库文件会使得生成的hex文件迅速增大。
一派護法 十九級
6樓 發表于:2015-12-19 19:30

【补充函数】
int str_replace_nth(char *find, char *replace, char **p, int n)
{
    int count = 0;
    while (n-- && str_replace_once(find, replace, p))
        count++;
    return count;
}
该函数可以指定替换字符串的次数。
比如替换两次:
str_reset(&str, "ababababca");
count = str_replace_nth("a", "{you}", &str, 2);
puts(str);
printf("Replacement count: %d\n", count);
输出:
{you}b{you}bababca
Replacement count: 2
请注意,代码中的&&两边条件的位置不能互换,也不能把n--改为--n。
一派護法 十九級
7樓 發表于:2015-12-19 19:32

最后,在程序结束时,一定要记得执行free(str)释放内存空间!

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:425 回複數:5
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2015-12-19 19:32
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。