In most cases, your correct answer would be 10 with your input. Not that 'c' is a word, but unless you're checking with a dictionary, you have to count it as one.

The indentation is very poor, but ++j, being done separately inside a for loop with j as the counter, is usually an error, right there. Why would you do that?

So "90210" isn't going to count as a word? "Fahrenheit 451" is only going to be one word? Better re-think that. Adjacent numbers have to be counted as a word if they have a space before them, and either punctuation (including newlines), or a space, after them.

You've Rube Goldberg'd this. All you need is a single for or while loop, with a few if statements inside.

In most cases, your correct answer would be 10 with your input. Not that 'c' is a word, but unless you're checking with a dictionary, you have to count it as one.

The indentation is very poor, but ++j, being done separately inside a for loop with j as the counter, is usually an error, right there. Why would you do that?

So "90210" isn't going to count as a word? "Fahrenheit 451" is only going to be one word? Better re-think that. Adjacent numbers have to be counted as a word if they have a space before them, and either punctuation (including newlines), or a space, after them.

You've Rube Goldberg'd this. All you need is a single for or while loop, with a few if statements inside.

That's it.

thanks for the reply, it was my fault not to say that we dont count numbers as words

new line characters, commas etc and punctuations will not be included in the string

also, why do you say that j++ is an error? i use it in order to make less checkings in the string

can you tell me exactly what you mean? maybe some extra code would help me understand

I can see that you don't count numbers as words. My point is that the book title "Fahrenheit 451" has more than one word in it's title.

True, but, i don't want to count numbers, that's why I did what I did and in this program if you have the fahrenheit 451 input, you will get 1 as a result, which is the correct result.

Originally Posted by Adak

Obviously, newlines, etc., are not included in the string, but they mark the end of a word.

As I know '\0' marks the end of a string, and i dont really need to know where it is cause I use strlen() function.

Originally Posted by Adak

"...word." <<== for example

as i said before, string can't and won't have punctuation, it's what the problem states.

Originally Posted by Adak

exercice
j++ is the counter in the for loop. Your logic is nearly always goofed if you are messing with the counter, inside a for loop. And since your word count is wrong...

why is it wrong? I get the correct results, I can't really understand what's wrong in my code, since for every kind of input, I get the correct answer, I had a sheet of paper with me while doing this, and I can say I'm 99% sure that my logic is not goofed.

you should make a check that j will not go over the lenght of the string

there are probabilities that you would get a segmentation fault or erroneous output

same check when you write

Code:

if (x[j+1]!=' ' && !isdigit(x[j+1])){

----------

adak already give a better solution, using a single loop, checking every step these things:
1 - if the actual character is a letter from 'a' to 'z' or from 'A' to 'Z', if not continue to loop
2 - increment the words_count every time you enter in a word (a series of letters of the alphabet)
3 - incrementing the index_loop and checking is not out of bound

One really, really easy way to think of a word is just as non-whitespace characters surrounded by spaces (or the ends of the string). With that in mind, I think something as simple as this might work for you . . .

Of course, there are other ways (perhaps even simpler ones). But hopefully that will give you some ideas. Note that I haven't test it, so no guarantees.

[edit] As others have suggested, it's probably better not to mess with the loop variable inside the loop, as I did (trying to make the code a little similar to yours). Another easy way to do this is to remember the character you saw during the last iteration of the loop (specifically, whether it was a space or not). Then just take note when you change from space to non-space, and from non-space to space. In the former case, you have the beginning of a word, so count it. For example:

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

ofcourse i wanted to change things, but you kept saying that my code and thinking was wrong without telling me why

I wanted you to *DISCOVER* what I was showing you - not be told, not by analyzing a function I wrote for you. Just take a few pointed suggestions, and with what you already know, you'd run right into it.

And be a bit amazed, perhaps.

If I had said your code and algorithm was great, why would you want to change it? What would be my argument to support that change?

That makes no sense to me.

I believe you should have taken my word for something - and gave it a shot.

Which is all OK, by the way. You don't know me, and there's no reason you should trust me to give you good advice on your program.