【发布时间】:2015-05-13 21:02:29
【问题描述】:
我编写了一个程序来获取一串单词,并根据出现的分隔符分隔每个单词并将其添加到数组中。
我已经对其进行了调整,以考虑到 ' ' , '.'或者 '。'。现在的目标是针对同时出现的多个分隔符进行调整(如“the dog,,,was walk”)并且仍然只添加单词。虽然我的程序可以工作,并且它不会打印出额外的分隔符,但每次遇到额外的分隔符时,它都会在输出中包含一个空格,而不是忽略它们。
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 = strlen(givenString); -
@karan,如果他们对代码的作用没有基本的了解,就很难帮助解决特定问题的人。问题是您执行
wordFiller的for将永远不会进入,因为您的current变量是一个'',并且您有一个&& 运算符,用于测试,它总是会返回为false,并且永远不要输入那段代码。 -
also:
wordFiller = malloc(stringCharCount);分配的字节太少了。 -
另外:
current != (' ' | '.' | ',')这不是C的工作方式。 -
@DrKoch 为什么分配的字节太少? wordFiller 每次循环时都会重置,将原始字符串中的下一个单词添加到其中。 strCharCount 将允许它最多存储 40 个字符,而 wordFiller 存储的字节数永远不会超过 7 个字节(百慕大或格林纳达)