fork download
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. void encrypt(char *text, int shift) {
  5. char *ptr = text;
  6. while(*ptr != '\0') {
  7. if(isalpha(*ptr)) {
  8. char base = islower(*ptr) ? 'a' : 'A';
  9. *ptr = (*ptr - base + shift) % 26 + base;
  10. }
  11. ptr++;
  12. }
  13. }
  14.  
  15. int main() {
  16. char text1[] = "hello world";
  17. encrypt(text1, 3);
  18. printf("Encrypted text: %s\n", text1);
  19.  
  20. char text2[] = "abc xyz";
  21. encrypt(text2, 5);
  22. printf("Encrypted text: %s\n", text2);
  23.  
  24. char text3[] = "abc xyz";
  25. encrypt(text3, 5);
  26. printf("Encrypted text: %s\n", text3);
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Encrypted text: khoor zruog
Encrypted text: fgh cde
Encrypted text: fgh cde