Wednesday, 30 November 2016

Print Staircase

Sample input 6
Sample Output 
     #
    ##
   ###
  ####
 #####
######
Solution-

int n = Convert.ToInt32(Console.ReadLine());
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= n; j++)
                {
                    if (j <= n - i)
                    {
                        Console.Write(" ");
                    }
                    else
                    {
                        Console.Write("#");
                    }
                }
                Console.WriteLine();
            }

Saturday, 26 November 2016

Thursday, 24 November 2016

Reverse a String word by word

Given a string, reverse it word by word.

input : I like to play with c-sharp.
expected output : c-sharp with play to like I.

  static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string[] arr = s.Split(' ');
            int length = arr.Length;

            // it will hold reverse array
            string[] revArr = new string[length];

            // this index holds index value for new reversed string.
            int index = length - 1;
            for (int j = length - 1; j >= 0; j--)
            {
                revArr[index-j] = arr[j];
            }

            string revString = String.Join(" ", revArr);
            Console.WriteLine(revString);

            Console.ReadLine();

        }



Count the length of last word in a String

Suppose you have a string "I love Programming"
Output - should be  11.
if there is no word then Output: 0

static void Main1(string[] args)
        {
            string s = Console.ReadLine();
            string[] arr = s.Split(' ');
            int length = arr.Length;
            Console.WriteLine(arr[length - 1].Length);
            Console.ReadLine();

        }

Wednesday, 23 November 2016

Practical Difference between Static class and Singleton Pattern

Static Class - 

1. static class can not implement interfaces or can not drive from class , it must drive from Object,
2. static class can not passed as a method parameter
3. static class is a sealed class

Singleton Pattern-

1.  Singleton  can  implement interfaces or can drive from class.
2.  Singleton  can  passed as a method parameter
3.. its a abstract class because private constructor

Wednesday, 16 November 2016

Addition of 2 numbers of more than 30 length or more

For example you have to numbers
num1 = 24454543532465465434345465465656546454
num2 = 97867543532465465434345465465656546454

you can not store these in int or long because of range, so how will you do.

Solution-
1. just store both numbers in Array
2. start sum of elements of array from last
3. if any 'carry' then forward it to previous.

Text File words count

Suppose you have a text file, I want total number of words and which word how many times came. How will you approach it?

Solution-
1. First I will read file as string.
2. I will split the string based on space and store in Array.
3. Take A dictionary and key will be word and value will be count for that word.
4. If same word or key repeats then increment the count by one.
5. Enjoy...