C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-18-2005, 05:58 PM   #1
Registered User
 
Join Date: Oct 2005
Posts: 53
caesar cipher help.

I want to add a few things to this caesar cipher program but am not exactly sure how. They are:

1. Before encryption move the first letter of the message to the last posistion.( "frog" would be "rogf")

2.replace each letter by a constantly increasing value of k. So,
if k is 3, replace the first letter of the message by the letter that comes 3 positions after it. Next,
replace the second letter of the message by the letter that comes 4 positions after it. Next, replace
the third letter of the message by the letter that comes 5 positions after it, and so on.

3.At the beginning ask the user the value they want to use to encrypt the code. (It is currently set up at 3) But, i want it to be an option for the user.

4. Let the code "wrap around" when i type in zebra instead of returning cheud, it gives a funky character for z.

Not sure how to do these, they seem simple i just cant come up with how to do them.


Code:
#include <stdio.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;
                printf( "Doing encrypt\n" );
	printf("\nPlease enter the text you wish to encrypt: ");
	
	fgets(buff, sizeof(buff), stdin);
	{	
		while ( buff[i] != '\0' )
			{
				buff[i] = buff[i] + 3;
				i++; 
				
			}
	}
		printf("\n Your encrypted text is: %s \n", buff);
}
void decode ( void ) 
{
	char buff[BUFSIZ];
	int i = 0;
                printf( "Doing decrypt\n" );
	printf("\nPlease enter the text you wish to decrypt: ");
	
	
	fgets(buff, sizeof(buff), stdin);
	{
		while ( buff[i] != '\0' ) 
			{
				buff[i] = buff[i] - 3;
				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;
}

Last edited by stormfront; 11-18-2005 at 06:00 PM.
stormfront is offline   Reply With Quote
Old 11-18-2005, 06:11 PM   #2
C++ Enthusiast
 
jmd15's Avatar
 
Join Date: Mar 2005
Location: MI
Posts: 532
In the include file algorithm, there are many useful functions that could help you. One of these being swap(). Used as such:
Code:
....
    char msg[25]={0};
    strcpy(msg,"Hello");
    swap(msg[0],msg[(strlen(msg)-1)]);
Now instead of msg holding Hello it will hold oellH. I would also check out some of the other algorithms available.
__________________
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
jmd15 is offline   Reply With Quote
Old 11-18-2005, 06:20 PM   #3
Registered User
 
Join Date: Oct 2005
Posts: 53
Quote:
Originally Posted by jmd15
In the include file algorithm, there are many useful functions that could help you. One of these being swap(). Used as such:
Code:
....
    char msg[25]={0};
    strcpy(msg,"Hello");
    swap(msg[0],msg[(strlen(msg)-1)]);
Now instead of msg holding Hello it will hold oellH. I would also check out some of the other algorithms available.

thx, but i just want it to swap the first letter to the end.
example: "hello" would be "elloh"
Is there an easy way to implement this ?
stormfront is offline   Reply With Quote
Old 11-18-2005, 06:32 PM   #4
C++ Enthusiast
 
jmd15's Avatar
 
Join Date: Mar 2005
Location: MI
Posts: 532
Oh nevermind, I thought you wanted to swap the two.
__________________
Trinity: "Neo... nobody has ever done this before."
Neo: "That's why it's going to work."
c9915ec6c1f3b876ddf38514adbb94f0
jmd15 is offline   Reply With Quote
Old 11-18-2005, 06:34 PM   #5
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Just use a loop and walk through your array replacing each current element with what is in the current element + 1. Of course you will want to save the original value of element 0 in a temp variable, and then when you are done 'shifting' all of your characters, you will want to place the character in temp in the last position in your array.

Give it a try, and post your code if you can't get it.
kermit is offline   Reply With Quote
Old 11-18-2005, 06:46 PM   #6
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,054
Is this what you wanted?

Code:
void swapit(char *pInput)
{
    char b, e;
    b = pInput[0];
    e = pInput[strlen(pInput)-1];
    pInput[0] = e;
    pInput[strlen(pInput)-1] = b;
}
BobS0327 is offline   Reply With Quote
Old 11-18-2005, 06:48 PM   #7
Registered User
 
Join Date: Oct 2005
Posts: 53
that appears to be what i need but when i try to put it in my code and call it, it doesnt do anything....maybe im doing wrong. Where does it go?

Last edited by stormfront; 11-18-2005 at 06:57 PM.
stormfront is offline   Reply With Quote
Old 11-18-2005, 07:01 PM   #8
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,054
Example...
Code:
#include <stdio.h>
#include <string.h>

void swapit(char *pInput)
{
    char b, e;
    b = pInput[0];
    e = pInput[strlen(pInput)-1];
    pInput[0] = e;
    pInput[strlen(pInput)-1] = b;
}
int main(void)
{
    char szSwap[] = {"Hello"};
    printf("%s\n", szSwap);
    swapit(szSwap);
    printf("%s\n", szSwap);
    return 0;
}
BobS0327 is offline   Reply With Quote
Old 11-18-2005, 07:10 PM   #9
Registered User
 
Join Date: Oct 2005
Posts: 53
hmm, no im still getting oellH using the swap function.

"Just use a loop and walk through your array replacing each current element with what is in the current element + 1. Of course you will want to save the original value of element 0 in a temp variable, and then when you are done 'shifting' all of your characters, you will want to place the character in temp in the last position in your array."


this seems like my best bet

Last edited by stormfront; 11-18-2005 at 07:13 PM.
stormfront is offline   Reply With Quote
Old 11-18-2005, 07:26 PM   #10
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,054
Possibly this???
Code:
#include <stdio.h>
#include <string.h>
char * swapit(char *pInput)
{
   static char szTemp[128];
    char b;
    b = pInput[0];
    memset(szTemp, 0, sizeof szTemp);
    strcpy(szTemp,pInput+1);
    szTemp[strlen(szTemp)] = b;
    return  (szTemp);
}
int main(void)
{
    char szSwap[] = {"Hello"};
    printf("%s\n", szSwap);
    printf("%s\n",swapit(szSwap));
    return 0;
}
BobS0327 is offline   Reply With Quote
Old 11-18-2005, 07:30 PM   #11
Registered User
 
Join Date: Oct 2005
Posts: 53
Thats it, but how do i get it to accept What the user inputs instead of hello?
stormfront is offline   Reply With Quote
Old 11-18-2005, 07:32 PM   #12
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Read the FAQ.

~/
kermit is offline   Reply With Quote
Old 11-18-2005, 07:47 PM   #13
Registered User
 
Join Date: Oct 2005
Posts: 53
im working on it...slowly

Last edited by stormfront; 11-18-2005 at 08:02 PM.
stormfront is offline   Reply With Quote
Old 11-19-2005, 03:29 PM   #14
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
Code:
char szSwap[] = {"Hello"};
You don't need the curly braces.

Code:
static char szTemp[128];
    char b;
    b = pInput[0];
    memset(szTemp, 0, sizeof szTemp);
You can just use ={0}.

Did you get it to work, stormfront?
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, etc.

New project: nort
dwks is offline   Reply With Quote
Old 11-19-2005, 05:07 PM   #15
Registered User
 
Join Date: Oct 2005
Posts: 53
No, still cant get it to work in the code. : (

i know i must be trying to put it in the wrong place
stormfront is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Caesar Cipher dldsob C++ Programming 7 07-06-2009 06:06 PM
Another Caesar Cipher Swordsman C++ Programming 6 09-07-2007 08:56 AM
About aes gumit C Programming 13 10-24-2006 03:42 PM
Help with Caesar cipher jcmichman C++ Programming 1 04-05-2005 10:50 AM
My little Caesar cipher emulator dead_cell C++ Programming 3 01-16-2004 01:05 AM


All times are GMT -6. The time now is 01:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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