gpt4 book ai didi

c - 重新分配后我得到一个断点。为什么?我怎样才能做对呢?

转载 作者:行者123 更新时间:2023-11-30 16:23:47 25 4
gpt4 key购买 nike

该程序的重​​点是用我的名字替换每一个连续的2个或更多声音。我怎样才能成功重新分配?

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

void main() {
char vocale[] = "AEIOUaeiou",
sir[] = "Aana are muaulte meiree.";
char *src = (char*)malloc(strlen(sir) + 1);
src = sir;
char name[] = "Marian";
int count = 0,
i = 0;
while (i < strlen(src)) {
if (strchr(vocale, src[i])) {
count++;
i++;
}
else {
if (count >= 2) {
src = (char*)realloc(src, strlen(src) + strlen(name) + 1);
insereaza(src, count, name, i);
i = i + strlen(name);
count = 0;
}
else {
count = 0;
i++;
}
}
}
puts(src);
_getch();
}

最佳答案

这是一个有一些更改的提案,这很困难,因为我不知道 insereaza 是做什么的

void main() {
const char * vocale = "AEIOUaeiou";
char * src = strdup("Aana are muaulte meiree."); /* must be in the heap for realloc */
const char * name = "Marian";
int srcLen = strlen(src);
int nameLen = strlen(name);
int count = 0, i = 0;

while (i < srcLen) {
if (strchr(vocale, src[i])) {
count++;
i++;
}
else {
if (count >= 2) {
src = (char*) realloc(src, srcLen + nameLen + 1); /* perhaps too large */
insereaza(src, count, name, i);
srcLen += nameLen; /* or srcLen += nameLen - count + 1 ? */
i += nameLen; /* or i += nameLen - count + 1 ? */
}
else {
i++;
}
count = 0;
}
}
puts(src);
_getch();
}

关于c - 重新分配后我得到一个断点。为什么?我怎样才能做对呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53894766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com