Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Caesar cipher code help!

$
0
0
The objective is to have the user input something like: "Hi, nice to meet you." and the code to be translated using a shift amount entered by the user. If h is shifted 3, the letter would be k. Here is my code thus far:
/* Caesar Cipher Project
   Input a message and a shift amount
   then prints out the encrypted message

   Programmer: Perry Chapman
   Date: 2/11/2013
*/

#include <stdio.h>
#include <string.h>

int main (void) 
{
  int key = 0;
  char input[80];
  int i;

  printf("Enter message to be encrypted: ");
  scanf("%s", &input);
  
  printf("\nEnter shift amount (1-25): ");
  scanf("%d", &key);
  
  for(i=0; input[i] != '\0';i++)
  {
      
    if (input[i]>='A' && input[i]<='Z')
    {
       input[i] = ((input[i] - 'A') + key) % 26 + 'A';
    }
    else if (input[i]>='a' && input[i]<='z')
    {
      input[i] = ((input[i] - 'a') + key) % 26 + 'a';
    } 
 
  }
  
  printf("\nEncrypted message: %s", input);
  
}



My issue is that the executable will ask for the input, and not ask for the shift amount. What can I do to solve this?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>