Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement