#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char * str = "Testing replace text...\n\n";
char* buffer = malloc(sizeof(char));
char* insertPoint = &buffer[0];
char* copy = str;
char* p = strstr(str, "epl");
char* g = "gard";
int size = 0;
size = p-copy; //p = 9, which is the number of elemts till the first element of the substring
//want to allocate this space, and then increment insertPoint, by that amt(it'll be pointing
// to nothing)
buffer = realloc(buffer, size);
printf("Size: %d\n", size);
memcpy(insertPoint, copy, size);
printf("COPY: %s\n", buffer);
copy += size;
buffer = realloc(buffer, size+strlen(g));
insertPoint += size;
printf("%c", *insertPoint);
memcpy(insertPoint, g, strlen(g)); //insert after the 9 letters, the string the size of g
size += strlen(g); //size if the size of the buffer
printf("Size2: %d\n", size);
printf("COPY2: %s\n", buffer);
return EXIT_SUCCESS;
}
只是一些快速的实验代码;我只是试图用“gard”替换str中的子串epl,但当我打印出来时,我正在打印的字符串缓冲区没有变化,这意味着第一个字符串im打印工作,它将所有字母放入缓冲区之前发生子字符串,但是当我尝试用子字符串替换时,它不起作用。我正在测试各个指针,它们看起来都是正确的......不确定发生了什么,有什么见解?谢谢......完全可运行的程序。
我认为代码中的问题是因为strlen
不包括终止零。我试图修复你的代码,但最后我发现重新编写它更容易(并使用更合理的变量名称)。
以下简单的四个步骤可行。 strlen
的连续使用可能会被变量所取代,但为了清楚起见,我将它们留下了。 (另外,一个好的编译器可以通过保留调用来很好地优化这段代码。)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *str = "Testing replace text...\n\n";
char* buffer;
char* find_str = "epl";
char* repl_str = "gard";
char *find_str_pos = strstr (str, find_str);
/* 1. create new buffer of the correct size */
buffer = malloc (strlen(str) - strlen(find_str) + strlen(repl_str) + 1);
/* 2. copy first part */
memcpy (buffer, str, find_str_pos - str);
/* 3. add new text */
memcpy (buffer + (find_str_pos - str), repl_str, strlen(repl_str));
/* 4. append original text */
memcpy (buffer + (find_str_pos - str) + strlen(repl_str), find_str_pos + strlen(find_str), strlen(find_str_pos) - strlen(repl_str) + 1);
printf ("-> [%s]\n", buffer);
return EXIT_SUCCESS;
}
替换“epl”后,您不会附加剩余文本“ace text ...”。
你能试试下面的代码吗?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
char *source_string = "Testing replace text...";
char *search_string = "epl";
char *replace_string = "gard";
int search_length = strlen(search_string);
int replace_length = strlen(replace_string);
// Find start position of search string
char *start = strstr(source_string, search_string); // start pointing to "eplace text..."
int intial_length = start - source_string; // intial_length = 9
// Get remaining text which should append after replace
char *remaining_string = (start + search_length); // remaining_string pointing to "ace text..."
int remaining_length = strlen(remaining_string); // remaining_length = 11
// Find total length of string after replacing text
int total_string_length = intial_length + replace_length + remaining_length; // 24
char *buffer = (char *)malloc(total_string_length + 1); // +1 for null pointer
char *current_index = buffer;
// Add initial text
memcpy(current_index, source_string, intial_length);
current_index += intial_length;
// Add replace text
memcpy(current_index, replace_string, replace_length);
current_index += replace_length;
// Add remaining text
memcpy(current_index, remaining_string, remaining_length);
current_index += remaining_length;
memcpy(current_index, "\0", 1); // add null pointer at last
printf("Final Output: %s", buffer); // Final Output: Testing rgardace text...
return 0;
}