[英]Find and replace all occurrences of a substring in C
我試圖在C中的字符串數組中查找和替換子字符串的所有出現。我認為我的大部分邏輯都沒有,但是我不知道其余部分的混亂之處。
這是相關的代碼-我要替換的字符串在searchStr
並且我試圖用replaceStr
替換它。 字符串數組稱為buff
。 我不需要在將修改后的字符串保存回數組后,只需將修改后的字符串打印到控制台即可。
for (size_t i = 0; i < numLines; i++) {
char *tmp = buff[i];
char finalStr[MAX_STR_LEN * 2];
char temporaryString[MAX_STR_LEN];
int match = 0;
while ((tmp = strstr(tmp, searchStr))) {
match = 1;
char temporaryString[MAX_STR_LEN];
char tmp2[MAX_STR_LEN];
printf("Buff[i]: %s", buff[i]);
sprintf(temporaryString, "%s", strstr(tmp, searchStr) + strlen(searchStr)); // Grab everything after the match
printf("Behind: %s", temporaryString);
strncpy(tmp2, buff[i], tmp - buff[i]); // Grab everything before the match
strcat(finalStr, tmp2);
printf("In Front: %s\n", finalStr);
strcat(finalStr, replaceStr); // Concat everything before with the replacing string
tmp = tmp + strlen(searchStr);
buff[i] = tmp; // Move buff pointer up so that it looks for another match in the remaining part of the string
}
if (match) {
strcat(finalStr, temporaryString); // Add on any remaining bytes
printf("Final: %s\n", finalStr);
}
}
如果那里有很多printf
,那么我可以看到所有要調試的地方。
示例案例:
如果我使用searchStr = 4
和replaceStr = !!!
對字符串what4is4this
運行它replaceStr = !!!
這是控制台中的輸出...我也使用//
添加注釋
Buff[i]: what4is4this // Just printing out the current string before we attempt to replace anything
Behind: is4this // Looking good here
In Front: hat // Why is it cutting off the 'w'?
Buff[i]: is4this // Good - this is the remaining string we need to look through
Behind: this // Again, looking good
In Front: hat!!!isat // It should be 'is'
Final: hat!!!isat!!!isat // final should be 'what!!!is!!!this'
有想法嗎? 我正在扯頭發試圖解決這個問題
謝謝!
這是指針擺弄和未定義行為的不健康組合,但是評論者已經告訴過您。 如果稍微簡化一下,並善用節儉的指針,則可以執行以下操作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// ALL CHECKS OMMITTED!
#define MAX_STR_LEN 1024
int main(int argc, char **argv)
{
char *buff, *cpbuff;
char *searchStr;
char *replaceStr;
// pointers too the two parts with the search string in between
char *tmp, *after;
// the final output (a fixed length is not good,
// should be dynamically allocated)
char finalStr[MAX_STR_LEN * 2] = { '\0' };
if (argc < 4) {
fprintf(stderr, "Usage: %s string searchstr replacestr\n", argv[0]);
exit(EXIT_FAILURE);
}
buff = malloc(strlen(argv[1]) + 1);
strcpy(buff, argv[1]);
searchStr = malloc(strlen(argv[2]) + 1);
strcpy(searchStr, argv[2]);
replaceStr = malloc(strlen(argv[3]) + 1);
strcpy(replaceStr, argv[3]);
// Keep a finger on the start of buff
cpbuff = buff;
while (1) {
printf("Buff: %s\n", buff);
// Grab everything after the match
after = strstr(buff, searchStr);
// No further matches? Than we're done
if (after == NULL) {
strcat(finalStr, buff);
break;
}
// assuming strlen(searchStr) >= 1
tmp = buff;
// mark the end of the first part
tmp[after - buff] = '\0';
// set the after pointer to the start of the second part
after = after + strlen(searchStr);
printf("Behind: %s\n", after);
printf("Before: %s\n\n", tmp);
// Put the first part to it's final place
strcat(finalStr, tmp);
// concat the replacement string
strcat(finalStr, replaceStr);
// Set buff to the start of the second part
buff = after + strlen(searchStr) - 1;
}
printf("Final: %s\n", finalStr);
// set the buff pointer back to it's start
buff = cpbuff;
free(buff);
free(searchStr);
free(replaceStr);
exit(EXIT_SUCCESS);
}
可以稱為濫用指針算術的唯一點是標記第一部分結尾的線。 可以通過測量和使用相關字符串的長度並對其進行算術來避免這種情況。 承認,它比較慢,因此取決於您的個別用例。
它仍然比我喜歡的復雜,但這是一個開始。
我希望您現在可以自己將其擴展到多行輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.