Hi!
I'm a beginner C# programmer, and our instructor has asked me to create a verification system for our web site so that a person has to complete a puzzle before they can fill out a form to submit comments on the site, etc.
What he wants us to do is to have a random sentence displayed such as "The cow jumped over the moon" or any sentence we want. I've already done this part.
But then, he wants us to make it so that the person must type in the fifth word in the sentence to complete the verification process.
My question is.. If you have a sentence like the one listed above, what code would one use to return just the fifth word in that sentence? The sentence is a string.
Hope someone can help.
From KN

I think an easy way to do this is get the index of the 4th and 5th space. then get the substring between them.

Hmmm..Not really sure how that will work exactly.. An example might be useful to look at so I can see what's happening, then try to take that and translate it for my own project. All I want to get is that fifth word

Hi,

You can use String.Split(" ") to get an array of words of your string, this way you can also know the number of words on that string. (if you plan to use multiple possible sentences)

Loren Soth

Hey!
Well, I did the string.split to create an array of words. Now how do I take that array of words and get the fourth word out of it and return it as a string?

yeah i like the split idea better.

string[] words = string.split(" ");
fifth word =words[4]

This article has been dead for over six months. Start a new discussion instead.