Created
August 24, 2017 10:23
-
-
Save ivorynoise/6fb91ddd8edf0060092d82e6e7c9377f to your computer and use it in GitHub Desktop.
Thug Life Code
This file contains 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 <cstdio> | |
#include <cstring> | |
#include <algorithm> | |
char code[1000]; | |
char inp[1000]; | |
int maxLen = 0; | |
void decode() { | |
//prints a pattern corresponding to code | |
//For even positions, a space is printed | |
//For odd positions, a ! is printed | |
//count is taken from code[i] | |
int idx = 0; | |
int curLen = 0; //length printed so far | |
bool isSpace = true; | |
while (code[idx] != '\0') { | |
//I have atleast one char to process | |
int cnt = code[idx]; | |
while (cnt) { | |
if (isSpace ) putchar(' '); | |
else putchar ('!'); | |
++curLen; | |
--cnt; | |
} | |
if (curLen == maxLen) { | |
curLen = 0; | |
putchar('\n'); | |
isSpace = false; //should always start with space | |
//F*** | |
} | |
isSpace = !isSpace; //toggle | |
++idx; | |
} | |
} | |
const char* encode() { | |
char* ptr = code; | |
while (fgets(inp, 990, stdin) != NULL) { //string has been read | |
//Caution! fgets appends \n to the inp. F*** | |
int curLen = strlen(inp) - 1; | |
maxLen = std::max(curLen, maxLen); | |
int i = 0; | |
int cnt = 0; | |
while (i < curLen) { | |
char c = inp[i]; | |
cnt = 1; | |
while (i < curLen && c == inp[++i]) { | |
++cnt; | |
} | |
//Lets write the count into a string in form of char | |
//So for 65 we write A and for 91 we write 0...the ascii codes! | |
sprintf(ptr++, "%c", cnt); | |
} | |
} | |
return code; | |
} | |
int main() { | |
// freopen("out", "r", stdin); | |
encode(); | |
decode(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment