Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,535 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,181 people online right now. Registration is fast and FREE... Join Now!




problems with caesar cipher in c

 
Reply to this topicStart new topic

problems with caesar cipher in c

Uber Fr0g
post 21 Nov, 2005 - 09:12 PM
Post #1


New D.I.C Head

*
Joined: 21 Nov, 2005
Posts: 3


My Contributions


Im writing a caesar cipher program in c. I have the program working alright, but in my encode function i am having a few problems.

1. I want the alphabet to "wrap" around so if the encod value is 2 z would be encoded as b.

2. I let the chooser pick the encode value. It works but it always puts a funny character at the end of the encoded word.

Can someone please help?

CODE

void encode ( void )
{
    char buff[BUFSIZ];
    int i = 0;
    int shift_value;

   printf( "Doing encrypt\n" );
    printf("\nPlease enter the text you wish to encrypt: ");
    fgets(buff, sizeof(buff), stdin);
    if ("%s"  > 'Z')
    {
  "%s" -= 'Z' - 'A';
    }
    printf("\nEnter your encryption shift value (anything from +-1 to 25): ");
   scanf ("%i", &shift_value);
    
    

    {    
 while ( buff[i] != '\0' )
     {
   buff[i] = buff[i] + shift_value;
   i++;
   
     }
    }
 printf("\n Your encrypted text is: %s \n", buff);
}
User is offlineProfile CardPM

Go to the top of the page


Uber Fr0g
post 21 Nov, 2005 - 09:22 PM
Post #2


New D.I.C Head

*
Joined: 21 Nov, 2005
Posts: 3


My Contributions


heres the whole code if that helps!

CODE

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

void menu ( void ) {
   printf("Please enter a number: \n"
          "1-Encrypt\n"
          "2-Decrypt\n"
          "3-Exit\n"
          "prompt > ");
   fflush( stdout );
}

int getchoice ( void ) {
   char buff[BUFSIZ];
   int  choice = 0;
   do {
       menu();
       if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
           /* success reading a line, does it make sense? */
           if ( sscanf( buff, "%d", &choice ) != 1 ) {
               printf( "Enter a number\n" );
           }
       } else {
           /* user EOF, just exit now */
           choice = 3;
       }
   } while ( choice < 1 || choice > 3 );
   return choice;
}

void encode ( void )
{
    char buff[BUFSIZ];
    int i = 0;
    int shift_value;

   printf( "Doing encrypt\n" );
    printf("\nPlease enter the text you wish to encrypt: ");
    fgets(buff, sizeof(buff), stdin);
    if ("%s"  > 'Z')
    {
  "%s" -= 'Z' - 'A';
    }
    printf("\nEnter your encryption shift value (anything from +-1 to 25): ");
   scanf ("%i", &shift_value);
    
    

    {    
 while ( buff[i] != '\0' )
     {
   buff[i] = buff[i] + shift_value;
   i++;
   
     }
    }
 printf("\n Your encrypted text is: %s \n", buff);
}
void decode ( void )
{
    char buff[BUFSIZ];
    int i = 0;
    int shift_value;
               printf( "Doing decrypt\n" );
    printf("\nPlease enter the text you wish to decrypt: ");
    fgets(buff, sizeof(buff), stdin);
    printf("\nEnter your encryption shift value (anything from +-1 to 25): ");
   scanf ("%i", &shift_value);

    {
 while ( buff[i] != '\0' )
     {
   buff[i] = buff[i] - shift_value;
   i++;
     }
    }
 printf("\nYour decrypted text is: %s \n", buff);
}



int main ( ) {
   int choice;
    while ( (choice=getchoice()) != 3 ) {
       if ( choice == 1 ) {

           encode();
       } else
       if ( choice == 2 ) {
           decode();
       }
   }
 

   return 0;
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 22 Nov, 2005 - 07:08 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


Are you getting this:
CODE

if ("%s"  > 'Z')
{
 "%s" -= 'Z' - 'A';
}

to compile correctly? If so, what compiler are you using? I'm surpised you are not getting an error about illegal assignments. Can you elaborate on your intention here?
User is offlineProfile CardPM

Go to the top of the page

Uber Fr0g
post 22 Nov, 2005 - 07:15 AM
Post #4


New D.I.C Head

*
Joined: 21 Nov, 2005
Posts: 3


My Contributions


That did not compile correctly, i was trying to get the alphabet to wrap around. If someone entered a z and the encode shift value is 2 then the z would be encoded as b, because the alphabet wraps around.
User is offlineProfile CardPM

Go to the top of the page

microchip
post 22 Nov, 2005 - 11:30 AM
Post #5


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37



Thanked 3 times
My Contributions


Pretty smart guy, Caesar.

Anyway, about the string problem. C as a language doesn't have any standard functions to compare or even handle strings. So you'll have to go past each char and test it to see if it's more than 'Z'.

CODE

int i = 0;
while (buff[i] != '\0') {
   buff[i] = buff[i] - ('Z' - 'A');
   i++;
}


Keep in mind that this trick only works if the char is uppercase. So you'll have to limit the input to be uppercase.

Oh PS, always something useful to keep handy... ASCII table

Oh PPS: I thought the Ceaser cipher was the one where he wrote all the letters in a square or something?

This post has been edited by microchip: 22 Nov, 2005 - 11:32 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 22 Nov, 2005 - 11:56 AM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


The original caesar cipher was a simple ROT13 encryption...Caesar wrote a message where every letter was replaced by it's counterpart 13 letters removed.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 01:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month