RenzCutie

Hello, World!

Oct 14th, 2021 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.42 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
  8.                                         www.onlinegdb.com
  9.                                         www.programiz.com
  10.  
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15.  
  16. int msgArrCnt;
  17. //makes the character uppercase
  18. char upprCase(char chr){
  19.     //if the character decimal value was 97 and above
  20.     if (chr >= 97)
  21.         chr = chr - 32;     //subtract 32 to match the decimal value of uppercase value in ASCII
  22.     return chr;
  23.    
  24. }
  25.  
  26. //get the length of string
  27. int strLength(char* chr){
  28.     int i = 0;
  29.     //count until the char[i] value is null
  30.     while (chr[i] != '\0')
  31.         i++;
  32.     return i;
  33. }
  34.  
  35. // (wala bang exponent function [^] sa C? Sorry nasanay sa Python at Java) exponent function
  36. int powerOf(int base, int exp){
  37.     int fnl = 1;
  38.     //repeatedly multiply the base with itself in [exp value] times  
  39.     int i;
  40.     for (i = 0; i < exp;i++)
  41.         fnl *= base;    // Ex: base = 2; exp = 3 : 2*2*2
  42.     return fnl;         //                       = 8
  43. }
  44.  
  45. // convert hex letters [A,B,C,D,E,F] into its decimal value [10,11,12,13,14,15,16]
  46. int hexLettertoInt(char letter){
  47.     //make the character uppercase, to be ok
  48.     int r = upprCase(letter);
  49.  
  50.     //if the letter is 'A' = 10, 'B' = 11, 'C' = 12, 'D' = 13, 'E' = 14, and 'F' = 15
  51.     return r - 55;      // Ex: 'A' in Hex = 65 ::  65 - 55 == 10
  52.  
  53. }
  54.  
  55.  
  56. int hexConvToInt(char* str){
  57.     // store the fnl variable
  58.     int fnl = 0;
  59.     // get the length of the string
  60.     int length = strLength(str);
  61.     // loop until the value of length
  62.     int i;
  63.     for (i = 0; i < length ; i++){
  64.         int curr = 0;
  65.         int crrIndx = length - i - 1;
  66.         // hex numbers are only 0 - 9, and the rest are A - F
  67.         if(str[i] >= '0' && str[i] <= '9'){
  68.             curr = str[i] - '0';        // ALWAYS REMOVE THE EMPTY CHAR to convert it into an integer
  69.            
  70.         } else {
  71.             curr = hexLettertoInt(str[i]);
  72.         }
  73.         fnl = fnl + (curr*powerOf(16,crrIndx));
  74.     }
  75.     return fnl;
  76. }
  77.  
  78.  
  79. void strRvrs(char *str)
  80. {
  81.     if (*str == '\0') // avoid empty char
  82.         return;
  83.    
  84.    
  85.     char *srtg ;
  86.     char *endg ;  
  87.  
  88.             // get the value of str from the memory then make it the starting point
  89.     for (   srtg = str,        
  90.             // end point from the memory [add the length and the loc of start then subtract 1 to remove empty char]
  91.             endg = strLength(str) - 1 + srtg;  
  92.             // loop until the start val and end val have met
  93.             srtg < endg;
  94.             // [increment] Ex: 0x7ffcbcb46de1 >> 0x7ffcbcb46de2
  95.             *srtg++,
  96.             // [decrement] Ex: 0x7ffcbcb46dec >> 0x7ffcbcb46ded
  97.             *endg--)        
  98.     {
  99.         // REVERSAL PROCESS
  100.         char tmp = *srtg;
  101.         *srtg = *endg;
  102.         *endg = tmp;
  103.  
  104.         //printf("%p %p\n", srtg, endg); Out: 0x7ffcbcb46de1 0x7ffcbcb46ded
  105.  
  106.     }
  107. }
  108.  
  109.  
  110.  
  111. int main(){
  112.     /*
  113.          _   _   _____   _       _        ___     __        __   ___    ____    _       ____  
  114.         | | | | | ____| | |     | |      / _ \    \ \      / /  / _ \  |  _ \  | |     |  _ \
  115.         | |_| | |  _|   | |     | |     | | | |    \ \ /\ / /  | | | | | |_) | | |     | | | |
  116.         |  _  | | |___  | |___  | |___  | |_| |     \ V  V /   | |_| | |  _ <  | |___  | |_| |
  117.         |_| |_| |_____| |_____| |_____|  \___/       \_/\_/     \___/  |_| \_\ |_____| |____/   by Renz.
  118.        
  119.        
  120.         using this code you can generate Hello, World!
  121.                     ||
  122.                    _||_
  123.                    \  /
  124.                     \/                                      */    
  125.     char code[] = "1422x6c76f7266x502c6f2c5x56c64x862x4";
  126.     int length = strLength(code);
  127.  
  128.  
  129.  
  130.  
  131.     // ASCII CHAR:  "H  e   l   l   o   ,     W  o   r   l   d   ! "
  132.     // HEXADECIMAL: "48 65  6c  6c  6f  2c 20 57 6f  72  6c  64  21";
  133.     // DECIMAL:     "72 101 108 108 111 44 32 87 111 114 108 100 33"
  134.     // 48656c6c6f2c20576f726c6421   >>  NORMAL
  135.  
  136.     // Decoding the code:
  137.     // First, the we should reverse the code
  138.  
  139.     strRvrs(code);  
  140.     // ANS: 4x268x46c65x5c2f6c205x6627f67c6x2241
  141.     //printf("%s \n",code);
  142.  
  143.     // Second, identify if there are 'x' char inside followed by an number,
  144.     // remove the chars 'xn' where n is the number then reverse next n numbers.
  145.     // Ex: ..x46c65.. == ..56c6..
  146.     char newCode[length];
  147.     int x, y, newLen;
  148.    
  149.     while (x < length){
  150.         if (code[x] == 'x' && code[x+1] >= '1' && code[x+1] <= '9'){
  151.             int revLen = code[x+1] - '0';
  152.            
  153.             //printf("::%i", revLen);
  154.             int y;
  155.             for (y = revLen + x + 1; y > x + 1; y--){
  156.                 // just making sure if the curr index is zero, to avoid random objects to be taken
  157.                 if (x == 0)
  158.                     // im a bit lazy now but this code will do the job copying each char in the newCode
  159.                     sprintf(newCode, "%c", code[x]);
  160.                 else
  161.                     sprintf(newCode, "%s%c", newCode, code[y]);
  162.                 /* printf("%c.\n",code[i]); */
  163.                 // counting the new length of the newCode
  164.                 newLen++;
  165.             }
  166.             //printf("%c%c-\n", code[x], code[x+1]);
  167.             x = revLen + x + 2;
  168.             continue;
  169.  
  170.         } else {
  171.             // just making sure if the curr index is zero, to avoid random objects to be taken
  172.             // copy the new code
  173.             if (x == 0)
  174.                 sprintf(newCode, "%c", code[x]);
  175.             else
  176.                 sprintf(newCode, "%s%c", newCode, code[x]);
  177.             // counting the new length of the newCode
  178.             newLen++;
  179.            
  180.         }
  181.        
  182.         x++;
  183.     }
  184.     // ANS: 48656c6c6f2c20576f726c6421
  185.     //printf("---%s---", newCode);
  186.     //printf("%s \n",code);
  187.  
  188.     int arrSize = length/ 2;
  189.     char tmp[64];
  190.     int tmpInt = 0;
  191.     int strIndx = 0;
  192.     int strCnt = 0;
  193.     int tmpIntArr[arrSize];
  194.  
  195.     // Let's start
  196.  
  197.     int i = -1;
  198.     while (i <= newLen){
  199.         tmp[strCnt-1] = newCode[i];
  200.         if (strCnt == 2){
  201.             tmpInt = hexConvToInt(tmp);
  202.             tmpIntArr[strCnt] = tmpInt;
  203.             printf("%c", tmpInt);
  204.             //printf("%c %i %i %i\n", tmpInt , tmpInt, strCnt,i/2);
  205.  
  206.             strIndx++;
  207.             strCnt = 0;
  208.         }
  209.        
  210.         i++;
  211.         strCnt++;
  212.     }
  213.     //printf("\n"); TEKA LANG DI KO ALAM KUNG NEED NG NEWLINE
  214.  
  215.  
  216.     /*
  217.     OUTPUT:
  218.  
  219.        ooooo ooooo ooooooooooo ooooo       ooooo         ooooooo        
  220.         888   888   888    88   888         888        o888   888o    
  221.         888ooo888   888ooo8     888         888        888     888      
  222.         888   888   888    oo   888      o  888      o 888o   o888  ooo
  223.        o888o o888o o888ooo8888 o888ooooo88 o888ooooo88   88ooo88    888
  224.                                                                   o88        
  225.  
  226.        oooo     oooo   ooooooo   oooooooooo  ooooo       ooooooooo     oo  
  227.         88   88  88  o888   888o  888    888  888         888    88o  8888
  228.          88 888 88   888     888  888oooo88   888         888    888  8888
  229.           888 888    888o   o888  888  88o    888      o  888    888   88  
  230.            8   8       88ooo88   o888o  88o8 o888ooooo88 o888ooo88     oo  
  231.  
  232.     */
  233. }
Add Comment
Please, Sign In to add comment