Reputation: 3
Okay, so I'm having trouble. I have a form that has three textboxes and a button, and in the first textbox (textBox1), the user inputs a sentence. In the next textbox (textBox3), the user inputs a word. Then the user clicks a button, and the third textbox (textBox2) checks to see if the word that is input matches a word in the sentence. I can't use any kind of "shortcuts", I can only go about it the long way (no .Compare or anything like that). Here's what I have so far:
private void button1_Click(object sender, EventArgs e)
{
string inSentence = textBox1.Text.ToUpper();
string txtB2 = textBox3.Text.ToUpper();
string[] inWord;
inWord = inSentence.Split(' ');
int wordCount = inWord.Length;
for (int i = 0; i < wordCount; i++)
{
if (txtB2 == inWord[i])
{
textBox2.Text = "Yes";
}
else
textBox2.Text = "No";
}
}
The problem I'm having is that, say if I type "hi its me" in the first box, the only word that will match is "me". It only matches the last word. It's not matching everything.
Again, I can only go about it in a way sort of like this. I just would like to know where I'm going wrong, and why. If anyone can help me, it would be much appreciated.
I've also tried using this
foreach (string n in inWord)
{
textBox2.Text = inWord + " ";
for (int i = 0; i < wordCount; i++)
{
if (txtB2 == n)
{
textBox2.Text = "Yes " + n;
}
else
textBox2.Text = "No " + n;
}
}
And am getting the same problem (I added the "n" to the text output to check what word it will be on and it's always on "ME" when I type in "hi its me").
Upvotes: 0
Views: 1626
Reputation: 849
Without using regular expressions or IndexOf
, and assuming you must use the very inefficient method of iterating through each word expressed below, you need to break execution of the loop upon finding a match. You should use a while construct rather than a for loop to accomplish this.
bool found = false;
int i = 0;
while (!found && i < wordCount)
{
if (txtB2 == inWord[i])
{
textBox2.Text = "Yes";
found = true;
}
else
textBox2.Text = "No";
i++;
}
Upvotes: 0
Reputation: 1852
What you are doing is looping through the entire sentence everytime. You check every word including the last one and the textBox2.Text is being changed at each word. What you see is the result at the last word.
textBox2.Text == "No"; // Initially set the text to "No"
for (int i = 0; i < wordCount; i++)
{
if (txtB2 == inWord[i])
{
textBox2.Text = "Yes"; // If a matching word is found change text to yes
break;
}
}
If you want to use a foreach
, you can leave out the other for
loop inside like so:
textBox2.Text = "No";
foreach(string word in inWord)
{
if(txtB2 == word)
textBox2.Text = "Yes";
}
The foreach
works by looping through inWord
and putting the value of the current element in word
Upvotes: 0
Reputation: 4135
If you're wanting a practical way to go about the problem, there's this:
var inSentence = textBox1.Text;
var wordToFind = textBox3.Text;
var wordFound = Regex.IsMatch(@"\b" + inSentence + @"\b", wordToFind, RegexOptions.IgnoreCase);
If this is a contrived school assignment, then 1. your teacher should probably come up with a better exercise and 2. you really need to be doing the work yourself.
Upvotes: 0
Reputation: 657
If the only restriction is that you not use regular expressions, then you can do something like this
var wordArray = textBox1.Text.ToUpper().Split(" ");
var response = wordArray.Where(x=> x.Trim().Equals(textbox2Input.ToUpper());
//the base implementation of the equals method on strings is enough
if(response.Any())
textbox3.Text ="Yes";
else
textbox3.Text="NO"
Upvotes: 0
Reputation: 1406
Try a regExp
string text = "Your sentence";
string pat = "[^\s]word[$\s]"; //[rowstart or whitespace]word[rowend or whitespace]
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
Match m = r.Match(text);
if(m.Success) {
// The sentence contains the word
}
Upvotes: 0
Reputation: 438
Problem: The "problem" in your code is that always every word is checked. When a match was found, the loop is not stopped.
Solution: add a return;
(or break
) statement in your if
statement after setting the textbox to "yes"
Upvotes: 1