# caesar shift

This is a discussion on caesar shift within the C Programming forums, part of the General Programming Boards category; ok i am trying to write a program that does a Caesar shift. i am doing the numbers first for ...

1. ## caesar shift

ok i am trying to write a program that does a Caesar shift. i am doing the numbers first for example if i have 1456 and i shift every number by 3 i'll get 4789.

so what i got so far is this. So the code below only works for one digit but i dont know how to make it do a whole like like above. any help would be appreciated. I know i have to use a loop but i am not sure how to do that.

Code:
```#include <stdio.h>

int main()
{

int temp,k,en,b;
char n;
scanf("&#37;c", &n);
printf("shift>");   // by how much should it be shifted
scanf("%d",&k); // assigns it to k

temp= (int)n + k; // converts to integer

if( '0'<=temp && temp <= '9')
en = temp;
else if( temp > '9')
en = temp - 10;
}
printf("%c", en); //prints the converted value of en from integer to char

;```

2. nevermind i figured it out

3. Originally Posted by jorgejags
ok i am trying to write a program that does a Caesar shift. i am doing the numbers first for example if i have 1456 and i shift every number by 3 i'll get 4789.
The caeser shift is an old school cypher, right? A little while ago someone gave me a t-shirt in ROT13, which is a caeser shift by 13 so that it works both encoding and decoding. This is what I came up with to read the shirt: rot13.c

It uses the lowercase ascii character values, so eg. you input rot13 "znex gjragl frira" to decode and "string" to encode.

Anyway, if you want to shift each digit (so if 1456 is 4789, would 4789 be 7012?) it will be way easier to do if you put the input into a character array instead of an int so you can operate on each one, which again might mean thinking of the ascii value -- subtract 48 to perform operations on a number and add 48 to return the char "number" to it's proper value:

Code:
```#include <stdio.h>
#include <string.h>

int main () {
short int i;
char num[]="1456";
for (i=0;i<strlen(num);i++) {
num[i]-=48;
num[i]+=3;
if (num[i] > 9) num[i]-=10;
num[i]+=48;
}
printf("&#37;s\n",num);
}```