# String Decode Problem

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.

Output List
aabc //a = 1, a = 1, b = 2, c = 3
kbc // since k is 11, b = 2, c= 3
alc // a = 1, l = 12, c = 3
aaw // a= 1, a =1, w= 23
kw // k = 11, w = 23

Solution:

```
class Crazyforcode
{

static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage: AllPossibleCodes.exe <number>");
return;
}
Process(args[0], "");
}

Process(args[0], "");

static 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());
}
}
}

}
```

### 0 Thoughts on “String Decode Problem”

1. Karthik Nedunchezhiyan on January 23, 2017 at 5:35 pm said:

It can be simply write in C like this:->
#include
int main()
{
char word[]=”1234″;
int i;
for(i=0;i<strlen(word);i++)
{
char temp= (char)((int)word[i]+49-1);
printf("%c",temp);
}
return 0;
}

2. //JAVA

public class TrickyQuestions {
public static void main(String[] args) {
System.out.println(numberToString(“09343″));
}

public static String numberToString(String numrat)
{
String shkronjat = “abcdefghij”;
String kthim = “”;

for (int i = 0; i < numrat.length(); i++){
kthim += shkronjat.toCharArray()[Character.getNumericValue(numrat.charAt(i))];
}
return kthim;
}
}