optimizing Caesar cipher analizer

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

55 Replies - 1627 Views - Last Post: 18 February 2013 - 05:14 AM Rate Topic: -----

#46 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 03:10 PM

Lines 25 to 36 should be deleted. Almost all of it is worse than nothing.

I gave you this code back in reply #6, some days ago. It's time you used it.
count[message[i]-'a']++;



Your algorithm should be:
for each char in the message
   if(isalpha(message[i])) {      //it's a letter or a number
      count[message[i]-'a']++;  //increment the count of that letter
   }  //where a equals the value of count[0], b the value of count[1], etc.
}



This may not handle every char, but it will handle 99% of them. The rest get special handling later.

Get that much done, and don't stop until the count is 2,2,2,2,2,2,2,2 ... for that earlier input.

This post has been edited by Adak: 17 February 2013 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#47 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 03:40 PM

thank you. The isalpha command is really usefull.
Sidenote, can you do something like isletter to detect upper/lowercase letters?
Was This Post Helpful? 0
  • +
  • -

#48 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 04:08 PM

it gets everything to lowercase, but still has the weird problem it had before

This post has been edited by adgjlsfhk: 17 February 2013 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

#49 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 04:25 PM

View PostSkydiver, on 17 February 2013 - 01:55 PM, said:

'a'-'a' == 0
'b'-'a' == 1
:
'z'-'a' == 25


This means that:
count[message[i]-'a']++;


is going to be sufficient to increment the count for a particular letter. But don't take my word for it. Try it out on paper. Notice that

notice that what?
Was This Post Helpful? 0
  • +
  • -

#50 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 04:35 PM

nevermind, I was doing something really stupid, and printing out message[i] instead of count[i]. Need a face-palm emoticon now.

it works, thanks.
Was This Post Helpful? 0
  • +
  • -

#51 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 05:08 PM

#include <stdio.h>
#include <ctype.h>

//looping
   unsigned int i=0;

//frequencies
   int count[25]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//greatest
   int greatest=0;
   int shift=0;

//program
int main(){

   char message[]="aabbccddeeEffGGHHIIJJKKLLmmnnooppqqrrSSTTUUVVWWXXYYZZ";

   for(i=0; message[i]!='\0'; i++){

      message[i]=tolower(message[i]);

      if(isalpha(message[i])){

        (count[(message[i])-'a'])++;
        }
   }

   printf("\n%s \n", message);

   for(i=0; i<25; i++){
      printf("%i, count[i]);
   }

//biggest
   for(i=0; count[i]<=25; i++){
      if(count[i]>greatest){
         greatest=count[i];

         shift=i;
      }
   }

   printf(\n"shift %i characters", (shift-4));

return 0;
}


here is my currently successful code with detector put back into it.
Was This Post Helpful? 0
  • +
  • -

#52 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 08:09 PM

You won't know what the shift is yet. In English, the most frequent letter is an 'e'. Doesn't always work (especially for shorter phrases), but it's a good guess to start with.

So shift is going to be the difference between the index of "greatest" (you call it "shift"), and 5.

First, put in a good message to be decoded - one that has been shifted already a known amount, and has more plain text 'e's in it, than any other letter. Then add the code to subtract, and find the true shift amount as described above. See if it matches.

We're not ready to decode yet, we're just checking if the code is working right, so far.

Post back when that's done, or if there's a problem.
Was This Post Helpful? 0
  • +
  • -

#53 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 09:15 PM

I tried it with shift amount being shift-5, but it was 1 off, so I switched it to 4. I also inverted the sign in preparation for decoding. Here is my current code with an attempt at a decoder based on a Caesar cipher code that I made right before starting this. It has some glitches which you'll find out if you run it.
#include <stdio.h>
#include <ctype.h>

//looping
   unsigned int i=0;

//frequencies
   int count[25]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//greatest
   int greatest=0;
   int shift=0;

//program
int main(){

   char message[]="please insert message here";

   for(i=0; message[i]!='\0'; i++){

      message[i]=tolower(message[i]);

      if(isalpha(message[i])){

        (count[(message[i])-'a'])++;
        }
   }

   printf("\n%s \n", message);

   for(i=0; i<25; i++){
      printf("%i", count[i]);
   }

//biggest
   for(i=0; count[i]<=25; i++){
      if(count[i]>greatest){
         greatest=count[i];

         shift=i;
      }
   }
   shift=-1*(shift-4);
   printf("\nshift %i characters\n", shift);

//decoder
   for(i=0; message[i]!=0x00; i++){

       if((message[i]+shift)<='z' && (message[i]+shift)>'a'){
//a-y
           message[i]+=shift;
       }else

//z
      if(message[i]<='z' && message[i]>'a'){
         message[i]=(message[i]-26)+shift;
      }

//printing
      printf("%c", message[i]);
   }
return 0;
}


This post has been edited by adgjlsfhk: 17 February 2013 - 09:17 PM

Was This Post Helpful? 0
  • +
  • -

#54 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 09:27 PM

Quote

It has some glitches which you'll find out if you run it.

Why should we have to run your program? Why can't you just tell us about your "glitches"? What is wrong with your program? Be specific.


Jim
Was This Post Helpful? 0
  • +
  • -

#55 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: optimizing Caesar cipher analizer

Posted 17 February 2013 - 09:33 PM

My mistake - frequency count for 'e' should be at count[4], not 5.

Make the message the alphabet, and let's see what letters are being shifted OK, and which are not.
I don't see any logic to "wrap back around" for char's near the end of the alphabet, but let's check and be sure, before proceeding.

First though add an = char to this:
    && (message[i]+shift)>'a'){ //should be >= surely



Was This Post Helpful? 0
  • +
  • -

#56 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: optimizing Caesar cipher analizer

Posted 18 February 2013 - 05:14 AM

It should be >=. When I first wrote it I didn't know how to do less than or equal, so I did less than one hex value higher. I tried to fix it up, but some stuff fell through the cracks.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4