fork download
  1. #include <stdio.h>
  2.  
  3. int isalphabet(char c){
  4. return(("A"<=c&&c<="Z")||("a"<=c&&c<="z"));
  5. }
  6.  
  7. char caesar(char c, int n){
  8. if(!isalphabet(c)) return c;
  9. int base = ("A"<=c&&c<="Z")?"A":"a";
  10. int pos = c-base;
  11. int shifted = (pos + n)%26;
  12. if(shifted<0)shifted+= 26;
  13. return base + shifted;
  14. }
  15.  
  16. void encrypt(char str[],int n){
  17. int i = 0;
  18. while(str[i]){
  19. str[i] = caesar(str[i],n);
  20. i++;
  21. }
  22. }
  23.  
  24. int main(void){
  25. char text[128];
  26. int shift;
  27.  
  28. printf("文字列を入力せよ:\n");
  29. scanf("%s",text);
  30.  
  31. printf("シフト数を入力せよ(正の数):\n");
  32. scanf("%d",&shift);
  33.  
  34. encrypt(text, shift);
  35. printf("暗号化:%s\n",text);
  36.  
  37. encrypt(text,-shift);
  38. printf("暗号化:%s\n",text);
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 5328KB
stdin
hello
stdout
文字列を入力せよ:
シフト数を入力せよ(正の数):
暗号化:hello
暗号化:hello