Created
October 21, 2021 10:12
-
-
Save pradhuman-soni/8c0900d2af43f2859b2b5a7d9a3971a0 to your computer and use it in GitHub Desktop.
pepcoding.com 21/10/2021 printEncodings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include<string> | |
| using namespace std; | |
| void printEncoding(string str, string asf){ | |
| if(str.length() == 0) { // base case | |
| cout << asf << endl; | |
| return; | |
| } | |
| int ch = str[0] - '0'; // picking first character and converting to integer | |
| string ros = str.substr(1); // substring staring from index 1 to pass in recursive call | |
| if(str.length() == 1) { // only work when string length is one i.e. single character | |
| printEncoding(ros, asf + (char)(ch + 'a' - 1)); | |
| } else { | |
| if(ch == 0) return; // handling invalid input 0 | |
| printEncoding(ros, asf + (char)(ch + 'a' - 1)); // first choice | |
| ch = stoi(str.substr(0, 2)); // picking 2 digit integer | |
| ros = str.substr(2); // now ros starts from index 2 | |
| if(ch <= 26) { | |
| printEncoding(ros, asf + (char)(ch + 'a' - 1)); // second choice | |
| } | |
| } | |
| } | |
| int main(){ | |
| string str; | |
| cin>>str; | |
| printEncoding(str,""); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include<string> | |
| using namespace std; | |
| void printEncoding(string str, string asf){ | |
| // write your code here | |
| } | |
| int main(){ | |
| string str; | |
| cin>>str; | |
| printEncoding(str,""); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment