fork download
  1. #include<stdio.h>
  2.  
  3. #include<ctype.h>
  4.  
  5. int main() {
  6.  
  7. char text[500], ch;
  8.  
  9. int key;
  10.  
  11. // Taking user input.
  12. printf("Enter a message to encrypt: ");
  13.  
  14. scanf("%s", text);
  15.  
  16. printf("Enter the key: ");
  17.  
  18. scanf("%d", & key);
  19.  
  20. // Visiting character by character.
  21.  
  22. for (int i = 0; text[i] != '\0'; ++i) {
  23.  
  24. ch = text[i];
  25. // Check for valid characters.
  26. if (isalnum(ch)) {
  27.  
  28. //Lowercase characters.
  29. if (islower(ch)) {
  30. ch = (ch - 'a' + key) % 26 + 'a';
  31. }
  32. // Uppercase characters.
  33. if (isupper(ch)) {
  34. ch = (ch - 'A' + key) % 26 + 'A';
  35. }
  36.  
  37. // Numbers.
  38. if (isdigit(ch)) {
  39. ch = (ch - '0' + key) % 10 + '0';
  40. }
  41. }
  42. // Invalid character.
  43. else {
  44. printf("Invalid Message");
  45. }
  46.  
  47. // Adding encoded answer.
  48. text[i] = ch;
  49.  
  50. }
  51.  
  52. printf("Encrypted message: %s", text);
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter a message to encrypt: Enter the key: Encrypted message: