HackerRank Challenge 2
Dictionary Map:
----------------------------------------------------------------------------------------------------
enter name and phone number and then search name in the given phone book list
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
Dictionary<string, string> phbook = new Dictionary<string, string>();
for (int i = 0; i < n; i++)
{
string str = Console.ReadLine();
char[] arr = str.ToCharArray();
StringBuilder name = new StringBuilder();
StringBuilder phone = new StringBuilder();
StringBuilder current = name;
for (int j = 0; j < str.Length; j++)
{
if (arr[j] != ' ')
{
current.Append(arr[j]);
}
else
{
current = phone;
}
}
phbook.Add(name.ToString(), phone.ToString());
}
int n1 = Convert.ToInt32(Console.ReadLine());
string[] names = new string[n1];
for (int i = 0; i < n1; i++)
{
names[i] = Console.ReadLine();
}
for (int i = 0; i < n1; i++)
{
if (phbook.ContainsKey(names[i]))
{
Console.WriteLine(names[i]+ "=" + phbook[names[i]]);
}
else
{
Console.WriteLine("Not found");
}
}
Console.ReadLine();
}
}
}
Comments
Post a Comment