fork(1) download
  1. #include <ctype.h>
  2. #include <stdio.h>
  3.  
  4. char code(char ch,int key) { return isalpha(ch)?(isupper(ch)?'A'+(ch-'A'+key)%26:'a'+(ch-'a'+key)%26):ch; }
  5.  
  6. char *encode(char *str,int key)
  7. {
  8. char *p;
  9. key%=26;
  10. if(key<0) key+=26;
  11. for(p=str;*p;++p) *p=code(*p,key);
  12. return str;
  13. }
  14.  
  15. char *decode(char *str,int key) { return encode(str,-key); }
  16.  
  17. int main()
  18. {
  19. int key;
  20. char bufor[1000];
  21. for(;;)
  22. {
  23. printf("podaj klucz i tekst: ");
  24. if(scanf("%d %999[^\n]s",&key,bufor)!=2) return 0;
  25. printf("input: \"%s\"\n",bufor);
  26. printf("encode: \"%s\"\n",encode(bufor,key));
  27. printf("decode: \"%s\"\n",decode(bufor,key));
  28. }
  29. }
Success #stdin #stdout 0s 2164KB
stdin
5 Ala ma kota
-5 Ala ma kota
5 Vgv hv fjov
-5 Fqf rf ptyf
stdout
podaj klucz i tekst: input:  "Ala ma kota"
encode: "Fqf rf ptyf"
decode: "Ala ma kota"
podaj klucz i tekst: input:  "Ala ma kota"
encode: "Vgv hv fjov"
decode: "Ala ma kota"
podaj klucz i tekst: input:  "Vgv hv fjov"
encode: "Ala ma kota"
decode: "Vgv hv fjov"
podaj klucz i tekst: input:  "Fqf rf ptyf"
encode: "Ala ma kota"
decode: "Fqf rf ptyf"
podaj klucz i tekst: