且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

你能帮我编码吗,我做不到

更新时间:2023-11-03 18:59:22

你所说的句子 通常被称为。所以我理解你想在字符串中找到第n个字并返回它的位置,即字符串中的字符索引。正确吗?



我们不会为你的作业提供代码,但是看到你正处于学习的最初阶段,一些提示可能会让你陷入困境正确的方向。



首先,我使用缩进格式化您的代码,以便更容易阅读。我使用了编辑框的琥珀色代码标签,让它以漂亮的琥珀色背景和等宽字体显示。在你的下一个问题中,你应该自己做。



现在,看看你的主要功能。它会调用你的WordIndex函数吗?不,那么世界上有什么用呢?



接下来,让我们来看看你的WordIndex函数。它返回void类型,即它不返回任何内容。 WordIndex不应该找到第n个字并返回其索引吗?然后我希望将WordIndex定义为:

What you denote as "sentence" is usually called a word. So I understand that you want to find the n-th word in a string and return its position, i.e. character index within the string. Correct?

We don't give you code for your homework, but seeing that you are at the very beginning of your study, a couple of hints might stear you in the right direction.

First, I formatted your code with indentation, so that it's easier to read. And I used the amber "code" tag of the edit box to let it appear with this nice amber background and in a monospaced font. In your next question you are expected to do that by yourself.

Now, take a look at your main function. Does it call your WordIndex function? No. So how in the world could that work?

Next, let's take a look at your WordIndex function. It returns type void, i.e. it doesn't return anything. Shouldn't WordIndex find the n-th word and return its index? Then I would expect WordIndex to be defined as:
int WordIndex (char *s)



我删除了size参数,因为它是多余的。 C字符串的末尾有一个NULL终结符,所以你逐个字符地读取s,直到你找到'\0'或者只是0.



现在有趣的东西。 WordIndex中的代码有点搞砸了。我希望你自己找到那些东西。我们将要使用的小秘密武器如下:将代码分组为2到10行的小包。每个包都应该做一些可以在评论中轻松描述的内容。按照该评论在每个组之前,如下所示:


I removed the size parameter as it is redundant. C-strings have a NULL-terminator at their end, so you read s character by character until you find '\0' or simply 0.

And now the interesting stuff. The code inside WordIndex is a little messed up. I want you to find those things by yourself. The little secret weapon we are going to use is the following: Group the code into little packages of 2 to 10 lines. Each package should do something that can easily be described in a comment. Precede each group by that comment, like this:

// find the first space in the string
char* p;
for(p = s; *p; ++p){
    if (*p == ' ')
        break;
}



通过详细描述每组线路应该做什么,我们可以逐步讨论代码中的任何故障 - 你甚至可以自己找到它们中的大部分。


By describing exactly what each group of lines is supposed to do, we can discuss any "glitches" in your code step by step -- and you will even find most of them on your own.


要做的第一件事是开始调用你的WordIndex函数 - 因为在你这样做之前,你将不知道它是否有效或者不是。



所以添加必要的代码来调用它,然后开始使用调试器来计算出现的情况。找出应该发生的事情,然后将每个阶段的情况与发生的情况进行比较。如果两者不一样的话,看看差异并找出原因。



但是我会启动你:pos中有什么值,k是第一个你比较它们的时间?为什么?



我不会告诉你什么是错的;我不打算为你修好。这不是残忍,而是教育。这是一项宝贵的技能,开发非常重要,因为它几乎是我们所做的一切的基础。学习它的***方法是练习它......诚实!
The first thing to do is start calling your WordIndex function - because until you do, you will have no idea if it works or not.

So add the necessary code to call it, then start using the debugger to work out what is going on. Work out what should happen, then compare that at each stage with what does happen. If the two are not the same, look at the difference and work out why.

But I'll start you off: what values are in pos and k the first time you compare them? Why?

I'm not going to tell you what's wrong; I'm not going to fix it for you. That isn't cruelty, it's education. This is a valuable skill, that it is seriously important to develop, because it is fundamental to pretty much everything we do. And the best way to learn it is to practice it... Honest!