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. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    19

    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;
              printf("Input: ");  //ask for input
              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. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    nevermind i figured it out

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jorgejags View Post
    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);
    }
    Google "ascii table" if you don't know what this is about.
    Quote Originally Posted by jorgejags View Post
    I know i have to use a loop but i am not sure how to do that.
    There's one right here.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > The caeser shift is an old school cypher, right?
    Indeed, rot13 can be made very secure by applying it twice
    Of course, you then have to do the reverse twice to get the original message back
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. Ceasar Shift program
    By trevordunstan in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 09:40 PM
  3. Caeser Cipher
    By L_U_K_E in forum C++ Programming
    Replies: 35
    Last Post: 06-30-2006, 07:15 AM
  4. Simulating Shift Key
    By PrimeTime00 in forum Windows Programming
    Replies: 7
    Last Post: 10-15-2004, 05:53 AM
  5. circular shift
    By n00bcodezor in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 02:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21