String Search
using System;
class ArraySearch
{
public void search(string[] a, string item) // Method for searching the 'item' in the given array 'a'
{
for (int i = 0; i < 5; i++)
{
if (a[i].Equals(item)) // Compares each element of the array with the item
{
Console.WriteLine("Item found at position: " + (i + 1));
return; // Returns when a matching position is found
}
}
Console.WriteLine("Item not found");// When no match is found
return;
}
static void Main(string[] args)
{
string[] myArray = { "A", "B", "C", "D", "E" }; // Target array
string item_1 = "A", item_2 = "Z"; // Elements to be searched
// Searching for item_1
Console.WriteLine("Item 1 search:");
ArraySearch j = new ArraySearch();
j.search(myArray, item_1);
// Searching for item_2
Console.WriteLine("Item 2 search:");
j.search(myArray, item_2);
Console.ReadLine();
}
}
Output:
Item 1 search:
Item found at position: 1
Item 2 search:
Item not found