SHARE
TWEET

rotn (rotate chars in str by n)

applehelpwriter Aug 26th, 2018 (edited) 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. [rotn Help]:
  3.  
  4. This (not very elegant) program transposes the letters in the provided string either by the number specified in the first parameter or, if that number is 0, for all 26 values of the alphabet.
  5.  
  6. TODO:
  7. Input values need to be sanitized.
  8.    
  9. Example usage:
  10.         rotn 13 hacker
  11.         -> unpxre
  12.         rotn 13 unpxre
  13.         -> hacker
  14.  
  15.         rotn 0 hacker
  16.         -> [1] ibdlfs
  17.         -> ...
  18.         -> ...
  19.         -> [26] hacker
  20.  
  21.  
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #define kAlphabet 26
  29.  
  30. char helpString[] = "[rotn Help]:\n\nThis program transposes the letters in the provided string either by the number specified in the first parameter or, if that number is 0, for all 26 values of the alphabet.\n\nTODO:\nInput values need to be sanitized.\n\nExample usage:\n\t\trotn 13 hacker\n\t\t-> unpxre\n\t\trotn 13 unpxre\n\t\t-> hacker\n\n\t\trotn 0 hacker\n\t\t-> [1] ibdlfs\n\t\t-> ...\n\t\t-> ...\n\t\t-> [26] hacker\n\n";
  31.  
  32. char transformWith(char c, int moveby, int upc)
  33. {
  34.     int pos = 0;
  35.     char *ults = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  36.     char *llts = "abcdefghijklmnopqrstuvwxyz";
  37.     char retval = c;
  38.     int startIndex = 0;
  39.     int i = 0;
  40.     for(i = 0; i < kAlphabet; i++)
  41.     {
  42.         char test;
  43.         if (upc > 0)
  44.         {
  45.             test = ults[i];
  46.         } else
  47.         {
  48.             test = llts[i];
  49.         }
  50.  
  51.         if (c == test)
  52.         {
  53.             startIndex = i;
  54.             pos = startIndex + moveby;
  55.             if (pos > 25)
  56.             {
  57.                 pos %= 26;
  58.             }
  59.             if (upc > 0)
  60.             {
  61.                 retval = ults[pos];
  62.             } else
  63.             {
  64.                 retval = llts[pos];
  65.             }
  66.         }
  67.     }
  68.     return retval;
  69. }
  70.  
  71. void rotateStr(char str[], int rb)
  72. {
  73.     int counter = 0;
  74.     for (counter = 0; str[counter] != '\0'; counter++)
  75.     {
  76.         int upc = 0;
  77.         char c = str[counter];
  78.         if (isupper(c))
  79.         {
  80.             upc = 1;
  81.         }  
  82.         printf("%c", transformWith(c, rb, upc));
  83.     }
  84. }
  85.  
  86. int main(int argc, char *argv[])
  87. {
  88.     int rotate;
  89.     if (argc == 3)
  90.     {
  91.         if (isdigit(*argv[1]))
  92.         {
  93.             int repeatTimes = 1;
  94.             char *end;
  95.             rotate = strtol(argv[1], &end, 10);
  96.             if (rotate == 0)
  97.             {
  98.                 repeatTimes = 26;
  99.             }
  100.             int r = 0;
  101.             for (r = 0; r < repeatTimes; r++)
  102.             {
  103.                 int rotateWith;
  104.                 if (repeatTimes == 1)
  105.                 {
  106.                     rotateWith = rotate;
  107.                 } else
  108.                 {
  109.                     rotateWith = r+1;
  110.                 }
  111.                 printf("[%d] ", rotateWith);
  112.                 rotateStr(argv[2], rotateWith);
  113.                 printf("\n");
  114.             }
  115.         }
  116.     } else
  117.     {
  118.         printf("\n%s", helpString);
  119.     }
  120.  
  121.    
  122.     return 0;
  123. }
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
 
Top