Friday 25 November 2016

Crazy Code (Part 2)

Decode String Problem

If a=1, b=2, c=3,….z=26. Given a string, find all possible codes that string
can generate. Give a count as well as print the strings.
For example:
Input: “1123″. You need to general all valid alphabet codes from this string.
what will be ?
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testCodes
{
    class Program
    {
        static void Main(string[] args)
        {
          Console.WriteLine("Enter number :");
          string nums  = Console.ReadLine();
          Program prog = new Program();
            prog.ProcessString(nums,"");
          
 
        }
      public  void ProcessString(string number,string code)
{
    if (number == null ||
        number.Length == 0)
    {
        if (code != null && code.Length > 0)
        {
            Console.WriteLine(code);
        }
        return;
    }
    ProcessString(number.Substring(1), code +
        (Convert.ToChar('a' + (number[0] - '0') - 1)).ToString());
    if (number.Length > 1)
    {
        int n = Convert.ToInt32(number.Substring(0, 2));
        if (n <= 26)
        {
            ProcessString(number.Substring(2), code +
                (Convert.ToChar('a' + n - 1)).ToString());
        }
    }
}
 
}
    }

this program work correctly but what you notice on output is it complete or not ?
if any one has question on code or misunderstand  i will answer in his comment
written by

No comments:

Post a Comment

Oracle requires a subscription to use Java SE NOW

May be licenses won’t be available for Java SE 8 or later, but separate support contracts also go away after Microsoft acquire G ithub...