none
List words not contained within my array? RRS feed

  • Question

  • I have an array with three 3~letter words : mat, far, let;

    A second array with a line of text asdmatredtletyxzc

    I've written code that compares the two array for similar characters and outputs their index in the line of text.

    I would like to know how I could notify myself if a word that is in my word bank is not in my line of text. I'd like to display it in the screen in this case. Far is not in the array. A simple solution that doesn't require libraries.


    StudiousStudent


    Friday, February 14, 2020 11:42 PM

Answers

  • I have an array with three 3~letter words : mat, far, let;

    A second array with a line of text asdmatredtletyxzc

    I would like to know how I could notify myself if a word that is in my word bank is not in my line of text. I'd like to display it in the screen in this case. Far is not in the array. A simple solution that doesn't require and c++ libraries would be ideal. 

    Based on earlier threads, I am assuming that this is part of a self-learning
    exercise and NOT a course assignment.

    This is the C# forum so I don't see how C++ libraries would apply.

    Here's a *very* quick example of one approach:

    static void Main(string[] args)
    {
        string[] sarr = { "mat", "far", "let" };
        string str = "asdmatredtletyxzc";
    
        foreach(var s in sarr)
        {
            bool found = false;
            for (int n = 0; n < str.Length - 2;  ++n)
            {
                if(s == str.Substring(n, 3))
                {
                    found = true;
                    break;
                }
            }
            if (found) found = false;
            else Console.WriteLine("The word \"{0}\" is not in the string.", s);                                              
        }
        Console.ReadLine();
    }
    

    - Wayne

    Saturday, February 15, 2020 12:12 AM

All replies

  • I have an array with three 3~letter words : mat, far, let;

    A second array with a line of text asdmatredtletyxzc

    I would like to know how I could notify myself if a word that is in my word bank is not in my line of text. I'd like to display it in the screen in this case. Far is not in the array. A simple solution that doesn't require and c++ libraries would be ideal. 

    Based on earlier threads, I am assuming that this is part of a self-learning
    exercise and NOT a course assignment.

    This is the C# forum so I don't see how C++ libraries would apply.

    Here's a *very* quick example of one approach:

    static void Main(string[] args)
    {
        string[] sarr = { "mat", "far", "let" };
        string str = "asdmatredtletyxzc";
    
        foreach(var s in sarr)
        {
            bool found = false;
            for (int n = 0; n < str.Length - 2;  ++n)
            {
                if(s == str.Substring(n, 3))
                {
                    found = true;
                    break;
                }
            }
            if (found) found = false;
            else Console.WriteLine("The word \"{0}\" is not in the string.", s);                                              
        }
        Console.ReadLine();
    }
    

    - Wayne

    Saturday, February 15, 2020 12:12 AM
  • 
        foreach(var s in sarr)
        {
            bool found = false;
            for (int n = 0; n < str.Length - 2;  ++n)
            {
                if(s == str.Substring(n, 3))
                {
                    found = true;
                    break;
                }
            }
    

    >if(s == str.Substring(n, 3))

    For added experience, you could/should write your own method that obtains the
    desired substring instead of using String.Substring. An example outline of 
    such a method might be:

    static string GetSubStr(string s, int idx, int len)
    {
        string ret = "";
        // get the substring from s using the arguments
        // ...
        // ...
        return ret;
    }
    

    I have avoided providing the complete method as you should be working through
    these exercises yourself.

    Calling it in my earlier posted example:

    for (int n = 0; n < str.Length - 2;  ++n)
    {
        //if(s == str.Substring(n, 3))
        if (s == GetSubStr(str, n, 3))
            {
            found = true;
            break;
        }
    }
    

    - Wayne

    Saturday, February 15, 2020 12:49 AM