Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Find a index of the array using Function and Pointer using the Char only (not string)

For example: I enter:

afghgbi kdjhiuth kjigunjt kjiurhg
2

The result must be the index of the 2nd sentence's index:
9 (2nd sentence is: kdjhiuth, where k-th index is 9)


There is my code, but i know here is several mistakes,can you please help me
C++
#include <iostream>
using namespace std;

void  WordIndex (char *s, int s_size);


int main()
{
    int index;
    char s[100];
    int k;
    cin.getline(s,100); 
    cin >> k;
    system("pause");
    return 0;
}

void  WordIndex (char *s, int s_size)
{
    int pos=1;
    int k;
    int index;
    for(int i=0; i<=s_size; i++){
        if (s[i] == ' '){
            pos++;
            break;
        }
    }
    while (pos == k){
        for (int i =0; i <= s_size; i++){
            if (k == s[i]){
                index = i;
            }
        }
    }
}
Posted
Updated 4-Sep-13 10:00am
v7
Comments
[no name] 4-Sep-13 15:01pm    
And? Was there maybe some sort of a question you wanted to ask? Or maybe describe some sort of a problem?
pasztorpisti 4-Sep-13 15:22pm    
Don't do programming if you don't like it or don't want to do it. That's it.
Member 10253112 4-Sep-13 15:53pm    
I didn't say that I don't want or I don't like... I want to learn more from my mistakes. That's it!
pasztorpisti 4-Sep-13 16:06pm    
The problem with this is that after reading a basic C tutorial you wouldn't have this question. http://www.cprogramming.com/
Member 10253112 4-Sep-13 15:50pm    
How can I find an index in n-th sentence? What code I should write?

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:
C++
int WordIndex (char *s)

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:
C++
// 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.
 
Share this answer
 
Comments
Member 10253112 5-Sep-13 14:41pm    
Thank you so much!=)
Menon Santosh 6-Sep-13 14:15pm    
my +5
nv3 6-Sep-13 15:07pm    
Thanks!
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!
 
Share this answer
 
Comments
Member 10253112 5-Sep-13 14:41pm    
Thank you so much!=)
Menon Santosh 6-Sep-13 14:15pm    
my +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900