Wednesday 2 June 2021

First Char Of Each Word ToUpper in C#

 public static string FirstCharOfEachWordToUpper(string textInfo, string seprator)

        {

            var spString = textInfo.Split(" ");

            var s1 = seprator == " " ? "_" : seprator == "_" ? " " : seprator;

            var result = "";

            foreach (var item in spString)

            {

                string firstLetterOfString = item.Substring(0, 1).ToUpper() + item.Substring(1);

                if (item != spString[spString.Length - 1])

                {

                    result += firstLetterOfString + s1;

                }

                else

                {

                    result += firstLetterOfString;

                }

            }

            return result;

        }

C# coding interview Link

 https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/


In this link you will get following  list of interview questions code

Q.1: How to reverse a string?

Q.2: How to find if the given string is a palindrome or not?

Q.3: How to reverse the order of words in a given string?

Q.4: How to reverse each word in a given string?

Q.5: How to count the occurrence of each character in a string?

Q.6: How to remove duplicate characters from a string?

Q.7: How to find all possible substring of a given string?

Q.8: How to perform Left circular rotation of an array?

Q.9: How to perform Right circular rotation of an array?

Q.10: How to find if a positive integer is a prime number or not?

Q.11: How to find the sum of digits of a positive integer?

Q.12: How to find second largest integer in an array using only one loop?

Q.13: How to find third largest integer in an array using only one loop?

Find Most Occurance Of Char In String in C#

 public static char? FindMostOccuranceOfCharInString(string value)

        {

            char? maxOccuranceChar = null;

            int maxOccuranceValue = 0;

            if (string.IsNullOrWhiteSpace(value))

                return null;

            char[] arr = value.ToLower().Trim().ToCharArray();

            Dictionary<char, int> _dic = new Dictionary<char, int>();

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

            {

                if (arr[i] != ' ')

                {

                    if (!_dic.ContainsKey(arr[i]))

                    {  _dic.Add(arr[i], 1);}

                    else

                    {   _dic[arr[i]]++;

                    }}}


            foreach (KeyValuePair<char, int> item in _dic)

            {

                if (item.Value > maxOccuranceValue)

                {

                    maxOccuranceChar = item.Key;

                    maxOccuranceValue = item.Value;

                } }


            return maxOccuranceChar;

        }

Check given string is palindrome or not

 public bool IsPalindrome(string value)  

        {    bool result = true;  

            if (string.IsNullOrEmpty(value))  

                return false;  

            _title = value.ToLower().Trim();  

            var min = 0;  

            var max = _title.Length - 1;  

            while (max >= 0)  

            {    if (value[min] == value[max])  

                {  min++;  max--; }  

                else  {  return false; }  }  

  

             return result; 

Get Reverse string in C#

  public string GetReverseString(string value)  

        {    string result = "";  

            if (string.IsNullOrEmpty(value))  

                return "";  

            for (int i = value.Length - 1; i >= 0; i--)  

            {  result += value[i];  

            }  return result;  

        } 

FindDuplicateChar or unique Char in C#

  public static StringBuilder FindDuplicateChar(string value)

        {  StringBuilder result = new StringBuilder();

            StringBuilder duplicateChar = new StringBuilder();

            foreach (var item in value)

            {  if (result.ToString().IndexOf(item.ToString().ToLower()) == -1)

                {   result.Append(item);}

                else

               { duplicateChar.Append(item);}

            }

           // return duplicateChar;   // Use for Duplicate

          // return result ;   // Use for unique

       }

Tuesday 1 June 2021

Find Majority Element in C#

 public class FindMajorityElementClass

    static void findMajority(int[] arr)

    {  int maxCount = 0; int index = 0;

       int n = arr.Length;

        for (int i = 0; i < n; i++) {

            int count = 0;

            for (int j = 0; j < n; j++) {

                if (arr[i] == arr[j])

                    count++;

            }

            if (count > maxCount) {

                maxCount = count;

                index = i;

            } }

 

        if (maxCount > n / 2)

            Console.WriteLine(arr[index]);

        else

            Console.WriteLine("No Majority Element");

    }

    static public void Main()

    {  int[] arr = { 1, 1, 2, 1, 3, 5, 1 };

        findMajority(arr); }}

Find second largest number in array in C#

 int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };

int largest = int.MinValue;

int second = int.MinValue;

foreach (int i in myArray)

{

 if (i > largest)

 {

  second = largest;

  largest = i;

 }

else if (i > second)

    second = i;

}


System.Console.WriteLine(second);

Find Second Smallest number in C#

 private static void FindSecondSmallest()

        {

            int[] elements = { 2, 5, 8, -5, -4, 0, 2, 10, 3, -3 };

            int smallest = int.MaxValue;

            int secondSmallest = int.MaxValue;

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

            {

                if (elements[i] == smallest)

                {

                    secondSmallest = smallest;

                }

                else if (elements[i] < smallest)

                {

                    secondSmallest = smallest;

                    smallest = elements[i];

                }

                else if (elements[i] < secondSmallest)

                {

                    secondSmallest = elements[i];

                }


            }

            Console.WriteLine(secondSmallest);

        }

Print duplicate item from array

  //Initialize array   

            int[] arr = new int[] {1,5,7,3,4, 9, 11, 15, 17, 1, 2, 3, 4, 2, 7, 8, 8, 3 };


            Console.WriteLine("Duplicate elements in given array: ");

            //Searches for duplicate element  

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

            {

                for (int j = i + 1; j < arr.Length; j++)

                {

                    if (arr[i] == arr[j])

                    {

                        Console.WriteLine("Array of i "+ arr[i]);

                        Console.WriteLine("Array of J "+arr[j]);

                    }

                }

            }

Screen Capture in C#

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.Threading;


 

int i = 0;

            do

            {

                i++;

                Bitmap bitmap = new Bitmap(1920, 1080);

                Graphics graphics = Graphics.FromImage(bitmap as Image);

                graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

                Random rnd = new Random();

                int rndNumber = rnd.Next(1, 10000);

                //var n1 = i + rndNumber;

                var ImgName = $"{i}_{rndNumber}_test";

                bitmap.Save($"E:\\FreeSoftwareTraining\\ScreenImg\\{ImgName}.jpeg", ImageFormat.Jpeg);

                Thread.Sleep(30000);

            }

            while (true);