0

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").

6 Answers 6

1

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"

Sign up to request clarification or add additional context in comments.

2 Comments

THAT ALSO WORKS! Wow. So I had everything working, and all I needed was to add the "break;" after setting the textbox to "Yes". At one point, I even added the break after the "No", and when it didn't work, I scrapped it.
Yes ;) it is also a good idea to have alternative solutions in mind, such as those mentioned in the other answers (linq, regex or other loop)
0

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++;
}

4 Comments

Why not using testing your sentence against a regexp like: "[^\s]word[$\s]"
This is probably a homework assignment, if I had to guess. Not sure why you posted this comment against my answer, as I added a disclaimer to address your exact question.
Sorry. My bad. Was commenting on the first post. :)
If you could accept the answer that would be great.
0

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
}

5 Comments

He specifically said I can't use any kind of "shortcuts", I can only go about it the long way. Although regExp is a great way to solve the issue, it is probably considered a shortcut.
Phyre is right. While I appreciate the help, Niclas, regExp is considered a shortcut. If I could've used it, I would've had this done a while ago. Thank you for your time, though. I appreciate it.
It's better to use word boundaries rather than looking for spaces (e.g. what if the word ends in punctuation). regular-expressions.info/wordboundaries.html I think RegEx is the right approach though. If the OP is otherwise constrained, then he's probably just trying to get others to do his schoolwork, in which case he doesn't deserve to have someone do it for him.
I actually never asked anybody to do it for me. I just wanted to know what I was doing wrong. Trust me, I would much rather do this myself so I could learn, and having someone answer it for you makes it harder to learn. All I wanted to know was where I was going wrong.
I hear ya, but the debugging process is part of learning to code. Have you been taught how to do step-through debugging using Visual Studio yet? It should have been fairly easy to tell what was going wrong if you set a breakpoint on the condition to see what was happening. Here's a quick overview. It should work for even the free editions of Visual Studio: msdn.microsoft.com/en-us/library/y740d9d3.aspx
0

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"

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.