gpt4 book ai didi

多个分隔符的代码核算不起作用

转载 作者:行者123 更新时间:2023-11-30 19:43:00 26 4
gpt4 key购买 nike

我编写了一个程序来获取一串单词,并根据出现的分隔符分隔每个单词并将其添加到数组中。

我已经对其进行了调整,以考虑“”、“。”或者 '。'。现在的目标是调整同时出现的多个分隔符(如“狗,,,正在散步”),并且仍然只添加单词。虽然我的程序可以工作,并且不会打印出额外的分隔符,但每次遇到额外的分隔符时,它都会在输出中包含一个空格,而不是忽略它们。

int main(int argc, const char * argv[]) {

char *givenString = "USA,Canada,Mexico,Bermuda,Grenada,Belize";

int stringCharCount;

//get length of string to allocate enough memory for array
for (int i = 0; i < 1000; i++) {
if (givenString[i] == '\0') {
break;
}
else {
stringCharCount++;
}
}



// counting # of commas in the original string
int commaCount = 1;
for (int i = 0; i < stringCharCount; i++) {
if (givenString[i] == ',' || givenString[i] == '.' || givenString[i] == ' ') {
commaCount++;
}
}


//declare blank Array that is the length of commas (which is the number of elements in the original string)
//char *finalArray[commaCount];



int z = 0;
char *finalArray[commaCount] ;
char *wordFiller = malloc(stringCharCount);


int j = 0;
char current = ' ';

for (int i = 0; i <= stringCharCount; i++) {

if (((givenString[i] == ',' || givenString[i] == '\0' || givenString[i] == ',' || givenString[i] == ' ') && (current != (' ' | '.' | ',')))) {
finalArray[z] = wordFiller;
wordFiller = malloc(stringCharCount);
j=0;
z++;
current = givenString[i];
}

else {
wordFiller[j++] = givenString[i];
}
}


for (int i = 0; i < commaCount; i++) {
printf("%s\n", finalArray[i]);
}
return 0;
}

这个程序花了我几个小时才聚集在一起(在更有经验的开发人员的帮助下),我不禁感到沮丧。我正在尽我最大的能力使用调试器,但肯定需要更多的经验。

/////////

我又回到了纸上,重写了我的代码。现在我尝试将分隔符存储在数组中,并将该数组的元素与当前字符串值进行比较。如果它们相等,那么我们遇到了一个新单词,并将其添加到最终的字符串数组中。我正在努力弄清楚我将用于此目的的“for”循环的位置和内容。

    char * original = "USA,Canada,Mexico,Bermuda,Grenada,Belize";

//creating two intialized variables to count the number of characters and elements to add to the array (so we can allocate enough mmemory)
int stringCharCount = 0;
//by setting elementCount to 1, we can account for the last word that comes after the last comma
int elementCount = 1;

//calculate value of stringCharCount and elementCount to allocate enough memory for temporary word storage and for final array
for (int i = 0; i < 1000; i++) {
if (original[i] == '\0') {
break;
}
else {
stringCharCount++;
if (original[i] == ',') {
elementCount++;
}
}
}

//account for the final element
elementCount = elementCount;

char *tempWord = malloc(stringCharCount);
char *finalArray[elementCount];
int a = 0;
int b = 0;
//int c = 0;
//char *delimiters[4] = {".", ",", " ", "\0"};

for (int i = 0; i <= stringCharCount; i++) {
if (original[i] == ',' || original[i] == '\0') {
finalArray[a] = tempWord;
tempWord = malloc(stringCharCount);
tempWord[b] = '\0';
b = 0;
a++;
}
else {
tempWord[b++] = original[i];
}
}

for (int i = 0; i < elementCount; i++) {
printf("%s\n", finalArray[i]);
}
return 0;
}

最佳答案

很多问题。建议将代码分成小块并先进行调试。

--

取消初始化数据。

// int stringCharCount;
int stringCharCount = 0;
...
stringCharCount++;

或者

int stringCharCount  = strlen(givenString);

还有其他问题:finalArray[] 从未分配过终止空字符,但 printf("%s\n", FinalArray[i]); 使用过。

char * 的使用不明确

char *wordFiller = malloc(stringCharCount);

wordFiller = malloc(stringCharCount);

关于多个分隔符的代码核算不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30225127/

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