-1

I am trying to get the strings from arrays to match, however, they only match when the length of the string is equal. I use split to make the string to an array.

For example, taking the string "The Quick Brown Fox" from the database will match a user input of "The Quick Brown Fox" but not "The Quick Brown Fox Jumps"

I want to match the string "The Quick Brown Fox" with "The Quick Brown Fox Jumps" and count the 4 words (The Quick Brown Fox) as correct and the "Jumps" as a wrong. Also, I need to match the EnteredWordsSplit if it is shorter than the WordsFromDatabaseSplit string.

        if (WordsFromDatabaseSplit.Length == EnteredWordsSplit.Length)
        {

            for (int i = 0; i < WordsFromDatabaseSplit.Length; i++)
            {
                if (WordsFromDatabaseSplit[i] == EnteredWordsSplit[i])
                {
                    correct++;
                }
                else
                {
                    wrong++;
                }
            }
            textBoxEnter.Text = "";
        }
        else
        {
            //code for matching
            textBoxEnter.Text = "";
        }
pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • 1
    ALthough the code snippet you have provided is not enough. I would suggest to use string.Contains(), from what I infer from your question. – Ehsan Jul 07 '14 at 04:22
  • check http://stackoverflow.com/questions/15301481/c-sharp-how-can-i-compare-two-word-strings-and-indicate-which-parts-are-differen – CodingDefined Jul 07 '14 at 04:27
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 07 '14 at 04:33

2 Answers2

0

try this

    public int ReturnRightCount(out int wrongCount)
    {
        var dbString = "The Quick Brown Fox";
        var inputString = "The Quick Brown Fox Jumps";
        var returnRight = 0;
        wrongCount = 0;

        var WordsFromDatabaseSplit = dbString.Split(' ');
        var EnteredWordsSplit = inputString.Split(' ');

        if (WordsFromDatabaseSplit.Length > EnteredWordsSplit.Length)
        {
            wrongCount = 0;
           return 0;
        }

        for (int i = 0; i < WordsFromDatabaseSplit.Length; i++)
        {
            if (WordsFromDatabaseSplit[i].Equals(EnteredWordsSplit[i],StringComparison.OrdinalIgnoreCase))
            {
                returnRight ++;
            }
            else
            {
                wrongCount++;
            }
        }

        return returnRight;
    }
}
Ashish Rajput
  • 1,489
  • 1
  • 11
  • 20
  • that doesn't help if it counts the entire thing as 0 – user3732623 Jul 07 '14 at 06:15
  • @user3732623 It's just an example to show that how can you do this, so you can very output i.e. wrongCount and returnRight based on your situations. If you have any other condition or i missed something here then let me know – Ashish Rajput Jul 07 '14 at 06:19
0

As suggested you could use Contains for easier matching. Now I am assuming that Database String could be found in Input String even if Input String is longer than Database String.

String dbString = "The Quick Brown Fox";
String inputString = "The Quick Brown Fox Jumps";
int correct=0, wrong=0;

if (inputString.Contains(dbString))
   {
      correct = dbString.Split(' ').Count(); // In our example the result is 4
      wrong = inputString.Split(' ').Count() - correct; // In our example the result is 1
   }
Edper
  • 9,144
  • 1
  • 27
  • 46