caesar cipher help.

This is a discussion on caesar cipher help. within the C Programming forums, part of the General Programming Boards category; I can get anything to work , it just messes my code up even more, not even worth posting cause ...

  1. #31
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I can get anything to work , it just messes my code up even more, not even worth posting cause it is evidently way wrong.

  2. #32
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Start simple. Make a program that reads an entire line. Compare that line with your phrase to stop looking on.
    Code:
    char buf[BUFSIZ] = {0};
    FILE *fp;
    
    fp = fopen( "yourfilename", "r" );
    if( fp == NULL )
        ...exit the program or whatever...
    
    while( fgets( buf, BUFSIZ, fp ) != NULL )
    {
        if( strstr( buf, "searchforthisphraseinbuf" ) == 0 )
            ...break from the loop to stop reading the file...
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #33
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    hmm,


    would something like this get me anywhere?
    Code:
    for( x = 0; buf[ x ] != '\0'; x++ )
    {
        if ((buf[x]>='A') && (buf[x]<='Z'))
        {
           something = 'A' + ((buf[x]-'A')+( something?? + x))%26;
         }
    }

    just to get me started.
    Last edited by stormfront; 11-22-2005 at 12:16 AM.

  4. #34
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    ok, im not getting anywhere with this so im concentrating on getting the alphabet to wrap around and letting the user choose the number to encode by.

    I have it where the user can choose the number and it works, but it places funny characters at the end of my words. I started trying to get the alphabet to wrap around but im getting erros can someone tell me whats wrong.

    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);
    }
    thats my encode function where i let the user choose the encode value, and it is also where im trying to wrap the alphabet around.

  5. #35
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Quote Originally Posted by quzah
    So in other words:
    Code:
    letter = letter + k + loop_counter
    Right? Now you should be able to figure out the implementation of this.


    Quzah.
    OMG that was so easy, ok, i figured this part out.

    IS there an easy way like that that i can move the first letter of the stuff to be encoded to the end? The things people have been posting are confusing the heck out of me.
    Last edited by stormfront; 11-22-2005 at 12:10 AM.

  6. #36
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    IS there an easy way like that that i can move the first letter of the stuff to be encoded to the end?
    Hint: strlen() returns the length of a string. Using
    Code:
    buffer[strlen(buffer)-1]
    will access the last character in the string.
    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, nort, etc.

  7. #37
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Here is something else to look at, although looking at it again after being a way a while it does look rather hideous.
    Code:
    char *bar(char *text)
    {
       static const char alpha[][26] = 
       {
          "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
          "abcdefghijklmnopqrstuvwxyz",
       };
       char *start = text;
       int n, k = 3;
       for ( ; *text; ++text )
       {
          if ( isalpha(*text) )
          {
             int set = islower(*text) ? 1 : 0;
             n = strchr(alpha[set], *text) - alpha[set] + k++;
             if ( n >= sizeof alpha[0] )
             {
                *text = alpha[!set][n % sizeof alpha[0]];
             }
             else
             {
                *text = alpha[set][n];
             }
          }
       }
       return start;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Caesar Cipher
    By dldsob in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2009, 06:06 PM
  2. Another Caesar Cipher
    By Swordsman in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 08:56 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Help with Caesar cipher
    By jcmichman in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2005, 10:50 AM
  5. My little Caesar cipher emulator
    By dead_cell in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 12:05 AM

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