SHARE
TWEET

gary.c

B1KMusic May 20th, 2018 (edited) 145 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Implementation of Gary's Formula as described on http://ludix.com/moriarty/psalm46.html
  2. /* v1.1 - slight revision/optimization:
  3.  * inlined get_code() and parse_arg() functions
  4.  * removed excessive multiplication (distributive property)
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <ctype.h>
  11.  
  12. uint64_t
  13. get_sum(char *arg)
  14. {
  15.     uint64_t sum = 0;
  16.  
  17.     for(; *arg; arg++)
  18.         sum += isalpha(*arg) ? toupper(*arg) - '@' : 0;
  19.  
  20.     return sum * 6;
  21. }
  22.  
  23. void
  24. parse_args(char **args)
  25. {
  26.     for(; *args; args++)
  27.         printf("%s: %lu\n", *args, get_sum(*args));
  28. }
  29.  
  30. void
  31. usage(void)
  32. {
  33.     fputs("usage: gary <string>\n", stderr);
  34.     exit(1);
  35. }
  36.  
  37. int
  38. main(int argc, char **argv)
  39. {
  40.     if(argv[1])
  41.         parse_args(argv + 1);
  42.     else
  43.         usage();
  44.  
  45.     return 0;
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top