+ 1
define an array with 10 words &takes a letter as input. Write a progto iterate through the array & output words contain no match
How to.write this prog
23 ответов
+ 26
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();
int count = 0;
int y =1;
while (
count < 9)
{
if(words[count].Contains(letter))
{
Console.WriteLine(words[count]);
y += count;
}
count ++;
}
if(y == 0 || y == 1)
{
Console.WriteLine("No match");
}
}
}
}
+ 10
static void Main(string[] args)
{
string[] words = {
"home",
"programming",
"victory",
"C#",
"football",
"sport",
"book",
"learn",
"dream",
"fun"
};
string letter = Console.ReadLine();
int count = 0;
//ваш код
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");
+ 5
Ayushi Mehta , sorry, but you gave a description to output all words that does NOT show the same as original task description.. ( found the task description).
But anyway, you should now be able to fix your code. I will mark my first answer as "Not correct".
+ 4
♨️♨️ , your code sample does work, but does not meet the task description from the tutorial:
- if a word is matching, the output should be this word (without anything else)
- if none of the words is matching, the output should be "No match"
Task desription:
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".
+ 2
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();
int count = 0;
for(int i=0; i<words.Length; i++){
string theWord= words[i];
if(theWord.Contains(letter)){
Console.WriteLine(theWord);
count++;
}
}
if(count==0){
Console.WriteLine("No match");
}
}
}
}
+ 1
Ayushi Mehta , small but important changes to your code:
EDITED: following code sample works fine, but does not output the expected result, as the description in original post was confusing.
...
while(count<9)
{
if(! words[count].Contains(letter)) // "letter" was a string, but should be variable name
Console.WriteLine(words[count]); // you only need to output words that does not contain the input letter, so no else is needed
count ++;
}
...
+ 1
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();
int count = 0;
int y =1;
while (
count < 9)
{
if(words[count].Contains(letter))
{
Console.WriteLine(words[count]);
y += count;
}
count ++;
}
if(y == 0 || y == 1)
{
Console.WriteLine("No match");
}
}
}
}
0
Sample Input : u
Expected Output : fun
0
According to your expected output your code is not correct in if condition u wrote if(words [count]. Contains ("letter")) here u need to write letter only without quotes.
May be you expecting this output
Simple input --enter any words which u mentioned and u have to find iteration.
0
//Try this one
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();
int count = 0;
while(count<10)
{
if(words[count].Contains(letter))
Console.WriteLine(count+"-->"+words[count]);
//else
// Console.WriteLine("No match");
count ++;
}
//your code goes here
}
}
}
0
Hii
0
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();
int count = 0;
//your code goes here
// string letter2 ="";
foreach (string itemw in words)
{
// count = itemw.IndexOf(letter);
if (itemw.Contains(letter))
{
Console.WriteLine(itemw);
count = 1;
}
}
if (count == 0 )
{
Console.WriteLine("No match");
}
}
}
}
0
//using foreach loop
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();
int count = 0;
//your code goes here
foreach(var item in words){
if(item.Contains(letter)){
Console.WriteLine(item);
count ++;
}
}
if(count==0)
Console.WriteLine("No match");
}
}
}
0
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();
int count = 0;
bool found = false;
for (count=0; count<=9; count++)
{
if (words[count].Contains(letter))
{found=true;
Console.WriteLine(words[count]);
}
}
if (found == false)
{
Console.WriteLine("No match");
0
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 = iConsole.ReadLine();
int count = 0;
int y =1;
while (
count < 9)
{
if(words[count].Contains(letter))
{
Console.WriteLine(words[count]);
y += count;
}
count ++;
}
if(y == 1)
{
Console.WriteLine("No match");
}
}
}
}
0
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 = Convert.ToString(Console.ReadLine());
int count = 0;
foreach (string value in words)
{
if(value.Contains(letter))
{
Console.WriteLine(value);
count++;
}
}if(count==0)
{Console.WriteLine ("No match");}
//your code goes here
}
}
}
0
*************Feel free and copy pest the code and try to understand**********
class Program
{
static void Main(string[] args)
{
string[] words = {
"home",
"programming",
"victory",
"C#",
"football",
"sport",
"book",
"learn",
"dream",
"fun"
};
string letter = Console.ReadLine();
int count = 0;
foreach (var word in words)
{
if (word.Contains(letter))
{
Console.WriteLine(word);
}
}
while (true)
{
if (words[count].Contains(letter))
{
break;
}
else count++;
if (count == 10)
{
Console.WriteLine("No match");
break;
}
}
}
}
0
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();
int count = 0;
int y =1;
while (
count < 9)
{
if(words[count].Contains(letter))
{
Console.WriteLine(words[count]);
y += count;
}
count ++;
}
if(y == 0 || y == 1)
{
Console.WriteLine("No match");
}
}
}
}
0
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();
int count = 0;
//your code goes here
foreach(var item in words){
if(item.Contains(letter)){
Console.WriteLine(item);
count ++;
}
}
if(count==0)
Console.WriteLine("No match");
}
}
0
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();
int count = 0;
foreach(var item in words){
if(item.Contains(letter))
{
Console.WriteLine(item);
count++;
}
}
if(count == 0) Console.WriteLine("No match");
}
}
}