Programming Interview Question

Program to print sum of given string where

a=0, b=1, c= a+b, d= b+c, e= c+d ….  z= x+y

Example:
Input:     a=0 b=1
               String “Question”

Output:  Sum of Given string : 15151


      int a, b;
            string strString = "abcdefghijklmnopqrstuvwxyz";
            int sum = 0;
            int indx;
            string strEntered;


            a = int.Parse(Console.ReadLine());
            b = int.Parse(Console.ReadLine());
            strEntered = Console.ReadLine().ToLower();

            int[] arr = new int[26];

            arr[0] = a;
            arr[1] = b;

            for (int i = 2; i < arr.Length; i++)
            {
                arr[i] = arr[i - 2] + arr[i - 1];
            }

            for (int i = 0; i < strEntered.Length; i++)
            {
                indx = strString.IndexOf(strEntered.Substring(i, 1), 0);
                sum += arr[indx];
            }

            Console.WriteLine("Sum of " + strEntered + " is :" +                        sum.ToString());

            Console.ReadLine();

Comments

Popular posts from this blog

iframe vs embed vs object in HTML 5

Constructor in c#

What is the need of method overriding ???