Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4.  
  5. char code[1000];
  6. char inp[1000];
  7. int maxLen = 0;
  8.  
  9. void decode() {
  10. //prints a pattern corresponding to code
  11. //For even positions, a space is printed
  12. //For odd positions, a ! is printed
  13. //count is taken from code[i]
  14.  
  15. int idx = 0;
  16. int curLen = 0; //length printed so far
  17. bool isSpace = true;
  18.  
  19. while (code[idx] != '\0') {
  20. //I have atleast one char to process
  21. int cnt = code[idx];
  22.  
  23. while (cnt) {
  24. if (isSpace ) putchar(' ');
  25. else putchar ('!');
  26. ++curLen;
  27. --cnt;
  28. }
  29.  
  30. if (curLen == maxLen) {
  31. curLen = 0;
  32. putchar('\n');
  33. isSpace = false; //should always start with space
  34. //F***
  35. }
  36.  
  37. isSpace = !isSpace; //toggle
  38. ++idx;
  39. }
  40. }
  41.  
  42. const char* encode() {
  43. char* ptr = code;
  44.  
  45. while (fgets(inp, 990, stdin) != NULL) { //string has been read
  46. //Caution! fgets appends \n to the inp. F***
  47. int curLen = strlen(inp) - 1;
  48. maxLen = std::max(curLen, maxLen);
  49.  
  50. int i = 0;
  51. int cnt = 0;
  52.  
  53. while (i < curLen) {
  54. char c = inp[i];
  55. cnt = 1;
  56. while (i < curLen && c == inp[++i]) {
  57. ++cnt;
  58. }
  59. //Lets write the count into a string in form of char
  60. //So for 65 we write A and for 91 we write 0...the ascii codes!
  61. sprintf(ptr++, "%c", cnt);
  62. }
  63. }
  64. return code;
  65. }
  66.  
  67.  
  68. int main() {
  69. // freopen("out", "r", stdin);
  70. encode();
  71. decode();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement