2

I'm working in a program for university and they use a problem like the typical Cesar Cipher. It's more like a functional program and needs to be the most basic possible.

The program will receive a number from the user from 65 to 90 and for example when the user inserts 65 will show 68. Will add 3 numbers but when the user gives 90 will give 67. 90+3 ---->90,65,66,67. It's a cycle from 65 to 90.

#include <stdio.h>

int cesar_encrypted(int x)
{
  return (x+3);
}


void test_cesar_encrypted(void)
{
    int x;
    scanf("%d", &x);
    int z = cesar_encrypted(x);
    printf("%s\n", z);
}

int main(){
    test_cesar_basic();

}

I did this sample code, but we can only go further and if you give 90 he will give 93 and I want 67.

Can anyone help me to wrap it around 90?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222

4 Answers4

4

You can use the modulo operator, it gives the remainder of a division:

int cesar_encrypted(int x)
{
  return (x - 65 + 3)%(91 - 65) + 65;
}

Implementing the suggestions of Sulthan (see comment), it would look like this:

int cesar_encrypted(int x)
{
  const int n_chars = 'Z' - 'A' + 1;
  const int shift = 3;
  return (x - 'A' + shift)%n_chars + 'A';
}
alain
  • 11,939
  • 2
  • 31
  • 51
  • `90 - 65 = 25`. But it should be modulus 26, because there are 26 characters in alphabet. Anyway, that's enough clue for him to do his homework. – Barmak Shemirani Sep 23 '15 at 10:24
  • I would use `'A'` instead of 65 and `'Z'` instead of 90. Also defining a constant `const int numCharacters = 'Z' - 'A' + 1` could simplify it a bit. Result: `return (x - 'A' + shift) % numCharacters + 'A'`. – Sulthan Sep 23 '15 at 10:32
1

Use modulo operation % to define the upper bound of the wanted interval and then use addition + to define the lower bound:

int cesar_encrypted(int x)
{ 
   // to wrap around 90  
   int encrypted = (x - 65 +3) % (90 - 65);
   // to start from 65, translate it adding 65  
   encrypted +=65;
   return encrypted;
}

or in a single line:

int cesar_encrypted(int x){  
   return  (x - 65 + 3) % (90 - 65)  + 65; // x in range [65,90]
}
Ziezi
  • 6,375
  • 3
  • 39
  • 49
1

First, let's define some constants to make the code more readable:

const int MIN_CHAR = 'A'; //equivalent to 65
const int MAX_CHAR = 'Z'; //equivalent to 90
const int NUM_CHARS = MAX_CHAR - MIN_CHAR + 1; //how many chars we have
const int SHIFT = 3; //how many characters we shift when ecrypting

now

int cesar_encrypted(int x) {
    if (x + SHIFT > MAX_CHAR) {
        return x + SHIFT - NUM_CHARS; //just subtract the number of chars.
    }

    return x + SHIFT;
}

which can be also written using the module operator as

int cesar_encrypted(int x) {
    return (x + SHIFT - MIN_CHAR) % NUM_CHARS + MIN_CHAR;
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • I'm afraid he wanted the program to be the "most basic", sir. – mtszkw Sep 23 '15 at 10:40
  • @MateuszKwasniak If you think "shorter" is "more basic" than you are wrong. – Sulthan Sep 23 '15 at 10:41
  • I do not, however I think there is no need for making code super-clear by declaring const variables (of course, it looks much better and it is more customizable, but it is just a homework I believe). – mtszkw Sep 23 '15 at 10:50
  • @MateuszKwasniak I write every program like production code. For me there is nothing like "it's just a homework", "it's just a unit test" etc. I think that we should always strive to make programs more readable. And students should especially learn how to do it. – Sulthan Sep 23 '15 at 11:29
  • yes i prefer the basic because i didnt learn the other things yet! Im new in programming and new in C. – Bruno Rodrigues Sep 23 '15 at 14:11
0

Just wrap to 65 if x+3 > 90 , else do nothing:

int cesar_encrypted(int x)
{
    return (x+3 > 90 ? ((x+3) % 90 + 64) : x+3);
}

You can see how it works, here: http://ideone.com/sunxTb
Of course you can simplify this to have a code without if statement (as other ppl mentioned):

return (x - 65 + 3)%(91 - 65) + 65;

In addition to that, you've got some minor typos in your code. Here's a type mismatching:

int z = cesar_encrypted(x);
printf("%s\n", z); // you are trying to print a string instead of int
mtszkw
  • 2,717
  • 2
  • 17
  • 31