有没有什么办法可以改善这一功能,取代了事件的子字符串用另一个字符串中的malloc分配的字符串? [英] Is there any way to improve this function which replaces occurrences of substrings with another string in an malloc allocated string?

查看:146
本文介绍了有没有什么办法可以改善这一功能,取代了事件的子字符串用另一个字符串中的malloc分配的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C,我决定做一个函数调用str_replace函数取代它已使用malloc字符串中的字符串。它似乎工作,但任何人都可以找到任何改进的余地。

任何意见将AP preciated。我想知道如果人们认为找到出现的次数来计算新的字符串大小是一个好主意,如果我使用指针是有道理的。

 的#include< stdlib.h中>
#包括< string.h中>
#包括< stdio.h中>

字符* str_replace函数(字符*字符串,字符*发现,字符*代替){
    //替换特定字符串的每个occurence一个malloc制字符串内用另一个字符串
    字符* POS =串;
    为size_t replace_size =的strlen(替换);
    为size_t find_size =的strlen(找到);
    了size_t过量为replace_size  -  find_size;
    //获取事件再度发生的数
    INT X = 0;
    而(1){
        POS =的strstr(POS,发现);
        如果(POS == NULL){
            打破;
        }
        POS ++;
        X ++;
    }
    如果(!X){//没有事件再度发生,以便与原字符串返回
        返回的字符串;
    }
    字符* new_string =的malloc(的sizeof(字符)*(strlen的(串)+超额* X + 1)); //加1为空终止
    POS =串; //重置指针
    字符* string_track =串; //用于走动的字符串。
    字符* new_string_begin = new_string; //新的字符串的起始返回
    而(1){
        POS =的strstr(POS,发现);
        如果(POS == NULL){
            的strcpy(new_string,string_track); //在剩余填写
            打破;
        }
        POS ++;
        为size_t seg_len = POS-string_track; //找到字符串和最后的长度
        的strncpy(new_string,string_track,seg_len); //复制串只是POS之前
        new_string + = seg_len  -  1; //去点进行更换,加入什么加入的指针。
        的strncpy(new_string,更换,replace_size);
        new_string + = replace_size; //什么去替换后指向
        string_track = POS + find_size  -  1; //原始字符串应该去点后发现串。
    }
    免费(串); //删除旧字符串
    返回new_string_begin; //返回新的字符串
}

INT主(INT ARGC,为const char * argv的[]){
    字符*字符串=的malloc(的sizeof(char)的* 21);
    的strcpy(字符串,不,是,否,是,否,是);
    的printf(%S \ N,字符串);
    字符串= strreplace(字符串,没有,没了);
    的printf(%S \ N,字符串);
    免费(串);
    字符串=的malloc(的sizeof(char)的* 21);
    的strcpy(字符串,不,是,否,是,否,是);
    的printf(%S \ N,字符串);
    字符串= strreplace(字符串,你好,没了);
    的printf(%S \ N,字符串);
    免费(串);
    字符串=的malloc(的sizeof(char)的* 21);
    的strcpy(字符串,不,是,否,是,否,是);
    的printf(%S \ N,字符串);
    字符串= strreplace(字符串,是,Y);
    的printf(%S \ N,字符串);
    免费(串);
    返回0;
}
 

解决方案

总的来说这是pretty的固体,有些事情我会建议将

1)不名的第一个参数串......我认为是一个有点冒险(不string.h中定义了一个串的象征?)

2)我不会释放旧字符串中的str_replace函数的功能,它不是由该函数分配的,因此它不应该释放它IMO,在这个例子中并不重要,但它通常是一个好习惯。这也意味着世界上没有必要在string_track变量,因为第一个参数是指向字符串的只是一个副本,你可以用它弄脏,只是不关心它最终导致它被扔掉时功能退出。

I'm very new to C and I decided to make a function called str_replace which replaces strings inside strings which have been made using malloc. It appears to work but can anyone find any room for improvements.

Any advice will be appreciated. I'd like to know if people think finding the number of occurrences to calculate the new string size was a good idea and if my use of pointers makes sense.

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

char * str_replace(char * string,char * find,char * replace){
    //Replaces each occurence of a particular string inside a malloc-made string with another string
    char * pos = string;
    size_t replace_size = strlen(replace);
    size_t find_size = strlen(find);
    size_t excess = replace_size - find_size;
    //Get number of occurences
    int x = 0;
    while (1) {
        pos = strstr(pos,find);
        if (pos == NULL){
            break;
        }
        pos++;
        x++;
    }
    if (!x){ //No occurences so return with original string
        return string;
    }
    char * new_string = malloc(sizeof(char)*(strlen(string) + excess*x + 1)); //Plus 1 for null termination
    pos = string; //Reset pointer
    char * string_track = string; //Used to move around string.
    char * new_string_begin = new_string; //Begining of new string to return
    while (1) {
        pos = strstr(pos,find);
        if (pos == NULL){
            strcpy(new_string,string_track); //Fill in remainder
            break;
        }
        pos++;
        size_t seg_len = pos-string_track; //Length between found string and last
        strncpy(new_string,string_track,seg_len); //Copy string to just before pos
        new_string += seg_len - 1; //Go to point for replacement by adding what was added to pointer.
        strncpy(new_string,replace,replace_size);
        new_string += replace_size; //Go to point after what was replaced
        string_track = pos + find_size - 1; //Original string should go to point after found string.
    }
    free(string); //Remove old string
    return new_string_begin; //return new string
}

int main (int argc, const char * argv[]) {
    char * string = malloc(sizeof(char)*21);
    strcpy(string,"No,yes,no,yes,no,yes");
    printf("%s\n",string);
    string = strreplace(string, "no", "nope");
    printf("%s\n",string);
    free(string);
    string = malloc(sizeof(char)*21);
    strcpy(string,"No,yes,no,yes,no,yes");
    printf("%s\n",string);
    string = strreplace(string, "hello", "nope");
    printf("%s\n",string);
    free(string);
    string = malloc(sizeof(char)*21);
    strcpy(string,"No,yes,no,yes,no,yes");
    printf("%s\n",string);
    string = strreplace(string, "yes", "y");
    printf("%s\n",string);
    free(string);
    return 0;
}

解决方案

Overall that's pretty solid, some things I would suggest would be

1) don't name the first argument "string" .. I think thats a little risky (doesn't string.h define a "string" symbol?)

2) I wouldn't free the old string in the str_replace function, it wasn't allocated by that function, so it shouldn't free it IMO, not really important for this example but it's generally a good habit. That would also mean theres no need for the "string_track" variable, since the first arg is just a copy of a pointer to a string, you can muck with it and just not care where it ends up cause it gets thrown away when the function exits.

这篇关于有没有什么办法可以改善这一功能,取代了事件的子字符串用另一个字符串中的malloc分配的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆