Skip to content

Commit

Permalink
facebook: prints all possible string codes of given numeric string
Browse files Browse the repository at this point in the history
  • Loading branch information
NirmalSilwal committed Aug 11, 2020
1 parent a8b8ae7 commit c78d93a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions CodingBlocks Training/Day11/codesOfString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package Lecture11;

public class codesOfString {

public static void main(String[] args) {

String inputStr = "1234";
printCodesOfString(inputStr, "");
}

public static void printCodesOfString(String str, String result) {
if (str.equals("")) {
System.out.println(result);
return;
}

String firstChar = str.substring(0, 1);
String restStr = str.substring(1);

char code1 = getCode(firstChar);

printCodesOfString(restStr, result + code1);

if (str.length() > 2) {
String twoChar = str.substring(0, 2);
String restStr2 = str.substring(2);

if (Integer.valueOf(twoChar) < 27) {
char code2 = getCode(twoChar);
printCodesOfString(restStr2, result + code2);
}
}
}

public static char getCode(String firstChar) {
int charValue = Integer.valueOf(firstChar);
int ascii = 96 + charValue;
return (char) ascii;
}
}

0 comments on commit c78d93a

Please sign in to comment.