optimizing Caesar cipher analizer

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

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

#1 adgjlsfhk  Icon User is offline

  • D.I.C Head

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

optimizing Caesar cipher analizer

Posted 08 February 2013 - 07:37 AM

I am new to programming and I made this code to figure out the shift amount to decode a Caesar cipher. The problem isn't that I am not getting errors, it's that it has so many if statements that it won't run. If anyone could give me help streamlining it at all, it would be greatly appreciated. I'm sorry that this code is so awfully optimized.

#include <stdio.h>

int i=0;
//looping

//frequencies
int count[26]= {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};

//most common
int greatest=count[0];

int value=count[1];

int main(void){

char message[]="insert message here";

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

if(message[i]==0x61){
count[0]++;
}else

if(message[i]=='a'){
count[0]++;
}else
if(message[i]=='b'){
count[1]++;
}else
if(message[i]=='c'){
count[2]++;
}else
if(message[i]=='d'){
count[3]++;
}else
if(message[i]=='e'){
count[4]++;
}else
if(message[i]=='f'){
count[5]++;
}else
if(message[i]=='g'){
count[6]++;
}else
if(message[i]=='h'){
count[7]++;
}else
if(message[i]='i'){
count[8]++;
}else
if(message[i]=='j'){
count[9]++;
}else
if(message[i]=='k'){
count[10]++;
}else
if(message[i]=='l'){
count[11]++;
}else
if(message[i]=='m'){
count[12]++;
}else
if(message[i]=='n'){
count[13]++;
}else
if(message[i]=='o'){
count[14]++;
}else
if(message[i]=='p'){
count[15]++;
}else
if(message[i]=='q'){
count[16]++;
}else
if(message[i]=='r'){
count[17]++;
}else
if(message[i]=='s'){
count[18]++;
}else
if(message[i]=='t'){
count[19]++;
}else
if(message[i]=='u'){
count[20]++;
}else
if(message[i]=='v'){
count[21]++;
}else
if(message[i]=='w'){
count[22]++;
}else
if(message[i]=='x'){
count[23]++;
}else
if(message[i]=='y'){
count[24]++;
}else
if(message[i]=='z'){
count[25]++;
}

}

//winner
for(i=0, greatest=0; i<26; i++){
value=count[i]
if (value > greatest){
	            greatest = value;
}
}

printf("shift %i characters", count[valaue]);

return 0;
}


This post has been edited by Skydiver: 08 February 2013 - 08:12 AM
Reason for edit:: Fixed ending code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: optimizing Caesar cipher analizer

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: optimizing Caesar cipher analizer

Posted 08 February 2013 - 08:17 AM

Look at this code:
char ch = 'D';
int index = tolower(ch) - 'a';
printf("%c's index is %d\n", index);



Does that give you any ideas on how to get rid of your long chain of if-else's?
Was This Post Helpful? 0
  • +
  • -

#3 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 08 February 2013 - 08:48 AM

What would 'a' minus 'a' equal?

What about 'b' minus 'a'?


What about charArray[i] - 'a'?

Oh yeah!
Was This Post Helpful? 0
  • +
  • -

#4 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 08 February 2013 - 08:57 AM

what does tlower do? I am really new and have never heard of it
Was This Post Helpful? 0
  • +
  • -

#5 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 08 February 2013 - 09:18 AM

This still won't run. Is it better at least?
#include <stdio.h>

int i=0;
int b=0;
//looping

//frequencies
int count[26]= {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};

//most common
int greatest=count[0];

int value=count[1];

int main(void){

char message[]="insert message here";

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

for(b=0; b!=74; b++){
if(message[i]=96+B)/>{
count[b]++;
b=74
}
}


//winner
for(i=0, greatest=0; i<26; i++){
value=count[i]


Was This Post Helpful? 0
  • +
  • -

#6 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 08 February 2013 - 10:02 AM

It's helpful if you change the uppercase (capital letters) to lowercase, so your frequencies will not be skewed. If you want to use tolower(char), then you use a loop

for(each letter of the message)
message[i]=tolower(message[i]);

and #include <ctype.h> as a header file, to your program.

You can use the same loop above, to count your frequencies. count[message[i]-'a']++ is the key concept. I know it looks "odd" or "twisted", but it's really elegant and efficient. If message[i] is an 'a', then count[0] will be incremented one time.

Pretty clever, eh? ;)
Was This Post Helpful? 0
  • +
  • -

#7 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 09 February 2013 - 01:45 PM

that will help. Thanks. Do you know why it doesn't run?
Was This Post Helpful? 0
  • +
  • -

#8 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 09 February 2013 - 02:07 PM

why can't you initialize a number to be equal to a variable?
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: optimizing Caesar cipher analizer

Posted 09 February 2013 - 02:13 PM

What do you mean?
Was This Post Helpful? 0
  • +
  • -

#10 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 09 February 2013 - 03:14 PM

it's giving me an error
error: initializer element is not constant
Was This Post Helpful? 0
  • +
  • -

#11 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 10 February 2013 - 02:45 AM

Line 11. You can't create a zero sized array:
int greatest=count[0];


Was This Post Helpful? 0
  • +
  • -

#12 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 10 February 2013 - 12:13 PM

http://codepad.org/nPpPRSSs
here's my most recent attempt.

it's giving me this I think error
Output:

1 Timeout

What does that mean?
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: optimizing Caesar cipher analizer

Posted 10 February 2013 - 02:20 PM

Post your code here, not to some random external site that may or may not be running a virus or a rick-roll.

My guess is that you have an infinite loop if that site is an online compiler and code runner.
Was This Post Helpful? 0
  • +
  • -

#14 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 10 February 2013 - 04:25 PM

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

int i=0;
int b=0;
//looping

//frequencies
int count[26]= {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};

//most common
int *greatest=&(count[0]);

int *value=&(count[1]);

int main(void){

char message[]="insert message here";

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

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

for(b=0; b!=74; b++){
if(message[i]=96+B)/>{
count[b]++;
b=74;
}
}


//winner
for(i=0, (*greatest)=0; i<26; i++){
*value=(*count+i);
if ((*value) > (*greatest)){
	            *greatest = *value;
}
}

printf("shift %i characters", count[value]);

return 0;
}



by the way, codepad is a completely legitimate website.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: optimizing Caesar cipher analizer

Posted 11 February 2013 - 08:10 AM

You need to find an indentation style you like and use it consistently. This will make finding this problem much easier.

In future please post the complete error messages, exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing the errors.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »