Shouldn’t it be string[] words = s.Split(' ');
Even if we say your word
is s.Split(' ');
then it should be string[] words = word
not string[]=word
And to count how much words are in that array just do int howManyWords = words.Length;
Also if you want to go through loop as many times as there are words in list you should do:
for(int i = 0; i < words.Lenght; i++)
{
//Do your stuff
}
While your question is very unclear (you should post what error you are getting at the least it would have made answering much easier) you cleared it up in the comments. What you are getting is an index out of range exception.
You need to change your loop
for (int i = 0; i <= word.length; i++)
To
for (int i = 0; i < word.length; i++) // preferable
or
for (int i = 0; i <= word.length - 1; i++) // not preferable
Arrays are index at 0 not 1. The length property will give you the total number of elements in the array. When you get to your last index the value of i will be greater than the number of elements because you started counting at 0.
You also need to change your while loop because you have an out of range exception happening there. Its the same problem except this time you added one to make it even worse.
while (test1 == false && ii != counter - 1)
Change it to a minus so it never goes out of range. Also change the logic to an && instead of || with the or when it finds a match it will never increment the ii so it will be stuck forever in the while.
You would also want to break out of the loop once you find the match.
Ok, so I found my problem. it was because there were no words in wordlist[] for it to read… my bad!
I should just place in a prevention method next time…
But thank you guys for your assistance! the code is now working as should!