简体   繁体   中英

Unable to iterate through this project

The program you are given defines an array with 10 words and takes a letter as input. Write a program to iterate through the array and output words containing the taken letter. If there is no such word, the program should output "No match".

Sample Input u

Sample Output fun

/////////////////////////////////////////The Code I did comes after this//////////////////
using System;
using System.Collections.Generic;

namespace Code_Coach_Challenge
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] words = {
                "home",
                "programming",
                "victory",
                "C#",
                "football",
                "sport",
                "book",
                "learn",
                "dream",
                "fun"
            };
            string letter = Console.ReadLine();
            for (int count = 0; count < 10; count++)
            {
                if (words.Contains(letter)) { Console.WriteLine(words[count]); }
                else
                {
                    Console.WriteLine("No match");
                }
            }
        }
    }
}

Since you use

  if (words.Contains(letter))

the application checks the whole array one item after other and compares the each whole word with the letter. If the letter is the whole world and it finds this world in one of array cells it returns true and the word words[count]. But words[count] maybe any word, not only the word that contains right letters. To return the right word you should use

 if ( words[count].Contains(letter))

And in this case application looking only for substring in each word, not for the whole word equals an letter.

So to find a right word in array you need to use array index. Try this

using System;
using System.Collections.Generic;

namespace Code_Coach_Challenge
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" };

            string letter = Console.ReadLine();
            var found = false;
            var i = 0;
            var list = new List<string>();
            for ( i = 0; i < words.Length; i++)
            {

                if (words[i].Contains(letter))
                {
                    found=true;
                    list.Add(words[i]);
                
                }
    
            }   
             if (found)  Console.WriteLine("Found word(s): "+ string.Join(",", list));
             else Console.WriteLine("No match");
        }
    }
}

output for input "o"

Found word(s):home,programming,victory,football,sport,book

UPDATE

More simple variant for students

 string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" };

            string letter = Console.ReadLine();
            var found = false;

            for ( var i = 0; i < words.Length; i++)
            {
                if (words[i].Contains(letter))
                {
                    found=true;
                    Console.WriteLine(words[i]);
                 }
              }   
             if (!found) Console.WriteLine("No match");

Try this

using System;
using System.Collections.Generic;

namespace Code_Coach_Challenge
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] words = {
                "home",
                "programming",
                "victory",
                "C#",
                "football",
                "sport",
                "book",
                "learn",
                "dream",
                "fun"
            };

            string letter = Console.ReadLine();
            bool found = false;
            
            
            for (int count = 0; count < words.Length ; count ++ )
            {
                if (words[count].Contains(letter))

                {
                    found = true;
                    Console.Write(words[count]);
                }
 
            }

            if (!found)
            {
              {Console.Write("No match");}
            }
            
        }
    }
}

using System;

namespace TestConsoleApp { internal class Program { private static void Main(string[] args) { string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" };

        string letter = Console.ReadLine();

        int count = 0;
        // Use the for loop below

        for (int a = 0; a < words.Length; a++)
        {
            if (words[a].Contains(letter))
            {
                Console.WriteLine(words[a]);
                count++;
            }
        }

        if (count == 0)
        {
            Console.WriteLine("No match");
        }
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM