Skip to content

Conversation

@linuxluser
Copy link

In your while-loop, you need to check that pos is not -1. -1 means that the substring (word in this case) was not found on that line. ANY positive integer means that word was found and thus is a match. So you only need to test pos against the value -1.

I also put in a check for the number of arguments given. If somebody runs your program without arguments, you should give a helpful error message and exit non-zero. Without that check, your program was simply illegally accessing the array argv and being killed by the system for an "out of bounds" exception. A quick check against argc makes this common situation much prettier.

In your while-loop, you need to check that `pos` is not -1. -1 means that the substring (`word` in this case) was not found on that line. ANY positive integer means that `word` was found and thus is a match. So you only need to test `pos` against the value -1.

I also put in a check for the number of arguments given. If somebody runs your program without arguments, you should give a helpful error message and exit non-zero. Without that check, your program was simply illegally accessing the array `argv` and being killed by the system for an "out of bounds" exception. A quick check against `argc` makes this common situation much prettier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant