【发布时间】:2012-10-12 10:51:45
【问题描述】:
我正在为计算机科学课做一些作业,而一项相当乏味的作业的最后一部分是编写一个可以反转句子的函数。教授提供的函数原型是这样的:
int reverseSentence(char** sentence, char **
newsentence, int maxWords)
...其中 sentence 是原始句子,newsentence 是我们应该转储倒置句子的位置,maxWords 是原始句子中的单词数。我这样写我的函数...
int reverseSentence(char** sentence, char **
newsentance, int maxWords)
{
int i = maxWords;
int x = 0;
while(i > 0){
newsentance[x] = sentence[i];
x++;
i--;
}
return maxWords;
}
但是,循环似乎永远在进行。另外,我似乎对如何使用char** 有误解。我以为它只是一个字符串数组,比如char[words][characters]。但是我收到有关以该形式将一组单词传递给函数的警告。我不是要求任何人为我做功课,只是为了澄清我在如何使用char** 方面所缺少的。
感谢任何帮助。谢谢。
P.S - 这就是我尝试测试我的代码的方式:
char sentence[3][4] = {"Hi\n", "my\n", "fri\n"};
char newsentence[3][4];
reverseSentence(sentence, newsentence, 3);
【问题讨论】:
-
假设代码没有崩溃(即传入的变量具有合理的值),循环应该终止。你为 maxWords 传递了什么值?
-
这就是我尝试在我的 main 中测试代码的方式:(在 P.S. 之后在原始问题中更新)
-
是句子,不是句子 :)