473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Caesar Cipher in C Help

I am taking a second semester programming course in C and have to write this program that I have to write a program that encrypts a message using a Caesar cipher. The user will enter the message to be encrypted and the value of the key. We are to assume that the message will not exceed 80 characters. We are supposed to include in the code something for the alphabet to wrap around like if the key is 2 then y would be replaced by a. The hint given for this is: to handle the wrap-around problem for upper-case characters, use the expression
((ch-‘A’) + k) % 26 + ‘A’
to calculate the encrypted version of an upper-case letter, where k is the key (i.e., the shift amount). You will need a similar expression for lower-case letters.
I am having a lot of trouble trying to figure this out, and we are supposed to be using arrays
when i try to compile i get error and cannot figure out what I am doing wrong#include <stdio.h>
#include <string.h>

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    int shift;
  4.    char message[80];
  5.    printf("Enter message to be encrypted: ");
  6.    scanf("%s", &message);
  7.    printf("Enter shift amount (1-25): ");
  8.    scanf("%d", &shift);
  9.  
  10.  
  11.    for(int i=0;i<strlen(message);i++)
  12.    {
  13.       if (message[i]=!' ')
  14.      if('A'>= message[i] <= 'Z')
  15.          {
  16.         message[i] = ((message[i]-'A') + shift) % 26 + 'A';
  17.      }
  18.      else if('a'>= message[i] <='z')
  19.      {
  20.             message[i] =((message[i]-'a') + shift) % 26 + 'a';
  21.      }
  22.    }
  23.    printf("Encrypted message: %s", message);
  24.    return 0;
  25. }
Mar 1 '11 #1
1 11363
jjdot
11
I think you should enable all warning messages of your compiler. gcc -Wall used on your program gives:

t.c:16:1: warning: suggest parentheses around assignment used as truth value
t.c:17:1: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning
t.c:21:1: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning
t.c:16:4: warning: suggest explicit braces to avoid ambiguous ‘else’

I think you have problems with C syntax:

disequality is != and not =!
x <= y <= z is written x <= y && y <= z


if (message[i]=!' ')
Mar 1 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Carl Harris | last post by:
I am trying to write some code to: 1.Prompt a user for filenames 2.Open the files 3.Convert my plain text into a cipher text array/string bear in mind I am a novice! I have wriiten some code...
1
by: beetle17 | last post by:
Plaintext: a  n i c e  d a y Key: -3 Ciphertext: X  k f Z b  a X v Cipher will accept commands from the user and perform the operations required by the commands. There are three different...
4
by: wagn31 | last post by:
i need to use a cipher but I have to used the assigned code in the ciphering i know how to do it, but i am not sure how to add my own dictionary. Here is what i have so far:
2
by: yeruvavasavi | last post by:
Algorithm 1: Write python programs to translate the following algorithm so that the computer can execute it. Algorithm for ceaser cipher. Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ<br> Cipher: ...
2
thatos
by: thatos | last post by:
How can a progrsm caesar cipher using python
1
by: happysuki | last post by:
Julius Caesar used this cipher with a cyclic shift of three to encrypt his message. In this case a is replaced by d, m by p and z by c. Hence ‘Julius Caesar’ will become ‘Mxolxv Fdhvdu’. Write a...
2
by: barks33 | last post by:
Hi all, im writing a caesar cipher and ive done most of the program (see below) def decrypt(ciphertext, shift): decrytped_text = " " for letter in ciphertext: if...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.