Tuesday, 23 February 2021

Finding the index of the string given without using any keywords

 Coding

 static void Main(string[] args)

        {

            string mainstring = "Generate reports for order wise and Product wise sales order reports";

            string secondstring = "e";

            GetPositions(mainstring, secondstring);

            string secondString = "e";

            Console.WriteLine(string.Join(", ", GetPositions(mainstring, secondString)));

            secondString = " ";

            Console.WriteLine(string.Join(", ", GetPositions(mainstring, secondString)));

            secondString = "wise";

            Console.WriteLine(string.Join(", ", GetPositions(mainstring, secondString)));

        }


Method GetPosition for the find the index


        public static int[] GetPositions(string mainstring, string secondString)

        { 

            List<int> listofsecondstring = new List<int>();

// find the single char index

            for (int i = 0; i < mainstring.Length; i++)

{

if(mainstring.Contains(secondString))

             {

                 if (secondString.Contains(mainstring[i]) && secondString == mainstring[i].ToString())

                 {

                     listofsecondstring.Add(i);

                 }

             }

                        

}

// To find the word index

            var words = mainstring.Split(' ');

            int z = 0;

            for (int J = 0; J < words.Length; J++)

            {

                z += words[J].Length ;

                if (secondString == words[J].ToString())

                {

                    listofsecondstring.Add(z);

                }

            }


            return  listofsecondstring.ToArray();

        }


        








Friday, 5 February 2021

Difference Between Dictionary And Hashtable In C#

 


Dictionary 
1.Dictionary is generic type Dictionary<TKey,TValue>
2.Dictionary class is a strong type < TKey,TValue > Hence, you must specify the data types for key and value.
3.There is no need of boxing/unboxing.
4.When you try to access non existing key dictionary, it gives runtime error.
5.Dictionary maintains an order of the stored values.
6.There is no need of boxing/unboxing, so it is faster than Hashtable.

"e.g Dictionary<string, string> EmployeeList = new Dictionary<string, string>();  
EmployeeList.Add(""Mahesh Chand"", ""Programmer"");  
EmployeeList.Add(""Praveen Kumar"", ""Project Manager"");  
EmployeeList.Add(""Raj Kumar"", ""Architect"");  
EmployeeList.Add(""Nipun Tomar"", ""Asst. Project Manager"");  
EmployeeList.Add(""Dinesh Beniwal"", ""Manager"");  "

Hashtable 
1.Hashtable is non-generic type.
2.Hashtable is a weakly typed data structure, so you can add keys and values of any object type.
3.Values need to have boxing/unboxing.
4.When you try to access non existing key Hashtable, it gives null values.
5.Hashtable never maintains an order of the stored values.
6.Hashtable needs boxing/unboxing, so it is slower than Dictionary.

"e.g Hashtable HT = new Hashtable();    
HT.Add(1,""s"");    
HT.Add(3, ""n"");    
HT.Add(4, ""j"");    
HT.Add(2, ""a"");    
HT.Add(5, ""u"");  "