RenzCutie

Hello, World v2

Oct 15th, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.30 KB | None | 0 0
  1. /*
  2.     Hello, World!
  3.     -by Renz Justine L. Villegas
  4.     October 15, 2021
  5.  
  6.     Created using:                      Tested using:
  7.     Visual Studio Code (LINUX)          Visual Studio Code (LINUX)
  8.                                         Dev-C++ v5.11 (WINDOWS)
  9.                                         www.onlinegdb.com
  10.                                         www.programiz.com
  11.                                         https://www.jdoodle.com/c-online-compiler/
  12.  
  13.     Execution time:
  14.         real    0m0.004s
  15.         user    0m0.003s
  16.         sys     0m0.001s
  17.  
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <time.h>
  22.  
  23. //makes the character uppercase
  24. char upprCase(char chr){
  25.     //if the character decimal value was 97 and above
  26.     if (chr >= 97)
  27.         chr = chr - 32;     //subtract 32 to match the decimal value of uppercase value in ASCII
  28.     return chr;
  29.    
  30. }
  31.  
  32. //get the length of string
  33. int strLength(char* chr){
  34.     int i = 0;
  35.     //count until the char[i] value is null
  36.     while (chr[i] != '\0')
  37.         i++;
  38.     return i;
  39. }
  40.  
  41. // (wala bang exponent function [^] sa C? Sorry nasanay sa Python at Java) exponent function
  42. int powerOf(int base, int exp){
  43.     int fnl = 1;
  44.     //repeatedly multiply the base with itself in [exp value] times  
  45.     int i;
  46.     for (i = 0; i < exp;i++)
  47.         fnl *= base;    // Ex: base = 2; exp = 3 : 2*2*2
  48.     return fnl;         //                       = 8
  49. }
  50.  
  51. // convert hex letters [A,B,C,D,E,F] into its decimal value [10,11,12,13,14,15,16]
  52. int hexLettertoInt(char letter){
  53.     //make the character uppercase, to be ok
  54.     int r = upprCase(letter);
  55.  
  56.     //if the letter is 'A' = 10, 'B' = 11, 'C' = 12, 'D' = 13, 'E' = 14, and 'F' = 15
  57.     return r - 55;      // Ex: 'A' in Hex = 65 ::  65 - 55 == 10
  58.  
  59. }
  60.  
  61.  
  62. int hexConvToInt(char* str){
  63.     // store the fnl variable
  64.     int fnl = 0;
  65.     // get the length of the string
  66.     int length = strLength(str);
  67.     // loop until the value of length
  68.     int i;
  69.     for (i = 0; i < length ; i++){
  70.         int curr = 0;
  71.         int crrIndx = length - i - 1;
  72.         // hex numbers are only 0 - 9, and the rest are A - F
  73.         if(str[i] >= '0' && str[i] <= '9'){
  74.             curr = str[i] - '0';        // ALWAYS REMOVE THE EMPTY CHAR to convert it into an integer
  75.            
  76.         } else {
  77.             curr = hexLettertoInt(str[i]);
  78.         }
  79.         fnl = fnl + (curr*powerOf(16,crrIndx));
  80.     }
  81.     return fnl;
  82. }
  83.  
  84.  
  85. void strRvrs(char *str)
  86. {
  87.     if (*str == '\0') // avoid empty char
  88.         return;
  89.    
  90.    
  91.     char *srtg ;
  92.     char *endg ;  
  93.  
  94.             // get the value of str from the memory then make it the starting point
  95.     for (   srtg = str,        
  96.             // end point from the memory [add the length and the loc of start then subtract 1 to remove empty char]
  97.             endg = strLength(str) - 1 + srtg;  
  98.             // loop until the start val and end val have met
  99.             srtg < endg;
  100.             // [increment] Ex: 0x7ffcbcb46de1 >> 0x7ffcbcb46de2
  101.             *srtg++,
  102.             // [decrement] Ex: 0x7ffcbcb46dec >> 0x7ffcbcb46ded
  103.             *endg--)        
  104.     {
  105.         // REVERSAL PROCESS
  106.         char tmp = *srtg;
  107.         *srtg = *endg;
  108.         *endg = tmp;
  109.  
  110.         // Out: 0x7ffcbcb46de1 0x7ffcbcb46ded
  111.  
  112.     }
  113. }
  114.  
  115.  
  116. // Let's do this!
  117. int main(){
  118.     /*
  119.          _   _   _____   _       _        ___     __        __   ___    ____    _       ____  
  120.         | | | | | ____| | |     | |      / _ \    \ \      / /  / _ \  |  _ \  | |     |  _ \
  121.         | |_| | |  _|   | |     | |     | | | |    \ \ /\ / /  | | | | | |_) | | |     | | | |
  122.         |  _  | | |___  | |___  | |___  | |_| |     \ V  V /   | |_| | |  _ <  | |___  | |_| |
  123.         |_| |_| |_____| |_____| |_____|  \___/       \_/\_/     \___/  |_| \_\ |_____| |____/   by Renz.
  124.        
  125.        
  126.         using this code you can generate Hello, World!
  127.                     ||
  128.                    _||_
  129.                    \  /
  130.                     \/                                      */    
  131.     char code[] = "BDCEKDBEFEIDIFCHEFCJIDEECGHEGFEDCCBA:805f7c6x241";
  132.     int length = strLength(code);
  133.  
  134.  
  135.  
  136.     // ASCII CHAR:  "H  e   l   l   o   ,     W  o   r   l   d   ! "
  137.     // HEXADECIMAL: "48 65  6c  6c  6f  2c 20 57 6f  72  6c  64  21";
  138.     // DECIMAL:     "72 101 108 108 111 44 32 87 111 114 108 100 33"
  139.     // BDCEKDBEFEIDIFCHEFCJIDEECGHEGFEDCCBA:805f7c6x241     >>  REVERSE THE WHOLE CODE
  140.     // 142x6c7f508:ABCCDEFGEHGCEEDIJCFEHCFIDIEFEBDKECDB     >>  SUPPLY THE CHAR FROM LEFT TO RIGHT
  141.     //             1422x6c76f7266x502c6f2c5x56c64x862x4     >>  REVERSE THE GIVEN INDEX
  142.     //             4x268x46c65x5c2f6c205x6627f67c6x2241     >>  REVERSE THE WHOLE CODE
  143.     //             48656c6c6f2c20576f726c6421               >>  NORMAL CODE
  144.  
  145.  
  146.  
  147.     // Decoding the code:
  148.     // We should reverse the code
  149.     // Reversal function of characters
  150.     strRvrs(code);  
  151.     // ANS: 142x6c7f508:ABCCDEFGEHGCEEDIJCFEHCFIDIEFEBDKECDB  
  152.  
  153.     char dCodedR[length];       char dCoded1[length];
  154.     int a = 0;                  int startA = 0;
  155.  
  156.     // Supply all the characters that stated from the left to the right
  157.     // Using the UPPERCASE characters in the ALPHABET as it's index.
  158.  
  159.     // find the ':' character then leave and copy the values in the dCodedR variable
  160.     while(code[a] != ':'){
  161.         // just making sure if the curr index is zero, to avoid random objects to be taken
  162.         if (a == 0)
  163.             // im a bit lazy now but this code will do the job copying each char in the variable
  164.             sprintf(dCodedR, "%c", code[a]);
  165.         else
  166.             sprintf(dCodedR, "%s%c", dCodedR, code[a]);
  167.         a++;
  168.     }
  169.     a++;    // jump an index, then start to copy the codes in dCoded1 variable
  170.  
  171.     // continue looping until we reach the length
  172.     while (a < length){
  173.         // just making sure if the curr index is zero, to avoid random objects to be taken
  174.         if (startA == 0)
  175.             // this code will do the job copying each char in the variable
  176.             sprintf(dCoded1, "%c", code[a]);
  177.         else
  178.             sprintf(dCoded1, "%s%c", dCoded1, code[a]);
  179.         startA++;   // count thr current index of the loop
  180.         a++;
  181.     }
  182.     //  ANS:
  183.     //  dCodedR: 142x6c7f508    dCoded1: ABCCDEFGEHGCEEDIJCFEHCFIDIEFEBDKECDB  
  184.  
  185.     int length1 = strLength(dCoded1);    int lengthR = strLength(dCodedR);
  186.     int l = 0;                           int r = 0;
  187.  
  188.     // DECODING THE CODE: from ALPHABET to ANOTHER CODE
  189.     // from 0 to the length of dCoder1
  190.     while (r < lengthR){
  191.         for(l = 0; l < length1; l++)
  192.             if(dCoded1[l] == 65 + r)        // A: 65, B: 64 ...
  193.                 dCoded1[l] = dCodedR[r];    // Replace the current char
  194.         r++;
  195.     }
  196.  
  197.     // Reverse function of characters
  198.     strRvrs(dCoded1);  
  199.     // ANS: 4x268x46c65x5c2f6c205x6627f67c6x2241
  200.  
  201.     // Identify if there are 'x' char inside followed by an number,
  202.     // remove the chars 'xn' where n is the number then reverse next n numbers.
  203.     // Ex: ..x46c65.. == ..56c6..
  204.     char dCoded2[length1];
  205.     int newLen = 0;     int x = 0;      int y = 0;
  206.    
  207.     while (x < length1){
  208.         if (dCoded1[x] == 'x' && dCoded1[x+1] >= '1' && dCoded1[x+1] <= '9'){
  209.             // get the length of the characters to be reversed
  210.             int revLen = dCoded1[x+1] - '0';
  211.             int y;
  212.            
  213.             // we should start from the current index plus one and reverse length
  214.             // then end it from the current index plus one, decrement.
  215.             for (y = revLen + x + 1; y > x + 1; y--){
  216.                 // just making sure if the curr index is zero, to avoid random objects to be taken
  217.                 if (x == 0)
  218.                     // will do the job copying each char in the dCoded2
  219.                     sprintf(dCoded2, "%c", dCoded1[x]);
  220.                 else
  221.                     sprintf(dCoded2, "%s%c", dCoded2, dCoded1[y]);
  222.                 // counting the new length of the dCoded2
  223.                 newLen++;
  224.             }
  225.             // to fix the index of the loop
  226.             x = revLen + x + 2;
  227.             continue;
  228.  
  229.         } else {
  230.             // just making sure if the curr index is zero, to avoid random objects to be taken
  231.             // copy the new code
  232.             if (x == 0)
  233.                 // will do the job copying each char in the dCoded2
  234.                 sprintf(dCoded2, "%c", dCoded1[x]);
  235.             else
  236.                 sprintf(dCoded2, "%s%c", dCoded2, dCoded1[x]);
  237.             // counting the new length of the dCoded2
  238.             newLen++;
  239.            
  240.         }
  241.         x++;
  242.     }
  243.     // ANS: 48656c6c6f2c20576f726c6421
  244.    
  245.     char tmp[64];       int arrSize = length1 /2;   int tmpIntArr[arrSize];
  246.     int tmpInt = 0;     int strIndx = 0;            int strCnt = 0;
  247.  
  248.     // Let's start decoding the last part.
  249.     // The current code was in hexadecimal, every 2 characters are in hex.
  250.     int i = -1;
  251.     while (i <= newLen){
  252.         tmp[strCnt-1] = dCoded2[i];
  253.         // count tayo 0 to 1 then if the strCnt is 2, then reset itself
  254.         if (strCnt == 2){
  255.             // conversion of the hex to int function
  256.             tmpInt = hexConvToInt(tmp);
  257.             // then the integer will convert into character
  258.             tmpIntArr[strCnt] = tmpInt;
  259.             // and print it one by one
  260.             printf("%c", tmpInt);
  261.  
  262.             strIndx++;
  263.             strCnt = 0;
  264.         }
  265.         i++;
  266.         strCnt++;
  267.  
  268.     }
  269.  
  270.     return(0);
  271.  
  272. }
  273.     /*
  274.     OUTPUT: Hello, World!
  275.  
  276.        ooooo ooooo ooooooooooo ooooo       ooooo         ooooooo        
  277.         888   888   888    88   888         888        o888   888o    
  278.         888ooo888   888ooo8     888         888        888     888      
  279.         888   888   888    oo   888      o  888      o 888o   o888  ooo
  280.        o888o o888o o888ooo8888 o888ooooo88 o888ooooo88   88ooo88    888
  281.                                                                   o88        
  282.  
  283.        oooo     oooo   ooooooo   oooooooooo  ooooo       ooooooooo     oo  
  284.         88   88  88  o888   888o  888    888  888         888    88o  8888
  285.          88 888 88   888     888  888oooo88   888         888    888  8888
  286.           888 888    888o   o888  888  88o    888      o  888    888   88  
  287.            8   8       88ooo88   o888o  88o8 o888ooooo88 o888ooo88     oo  
  288.  
  289.     */
Add Comment
Please, Sign In to add comment