I am writing a program to find the solution of a shift cipher without knowing the key.The code is around the length of 400 words without any signs like (, . ! :) and all in capital letters. There are two part in my program, the first one is to output all 25 possible solutions and separately store inside an array. The first part have no errors and can work just fine. The second part is to find in the 25 possible solution with the most character "E" "A" "R" "I" "O", than print the 5 possible solution. Thats where the problem occurred, if i enter something like "VDDDDDDDDC" it will start printing the wrong code. Here is my program. (The switch choice is for further function which i will work on it later, the one which have errors is the first one.) (Also if there are better ways to do it beside spamming if please let me know :D) (I am pretty new at c so explain would be really helpful):D
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char code[400];char after[400];char store[25][400]; // variable initialization
//store form 1 not 0
int f,x,choice,z,temp,y,q,e=0,a=0,r=0,i=0,o=0;
int ce=0,ca=0,cr=0,ci=0,co=0;
int se=0,sa=0,sr=0,si=0,so=0;
printf(" Welcome\n");
printf("Enter your code here:\n");
scanf("%[^'\n']s",code);
printf("Press 1 to Decrypt\nPress 2 to encrypt\nPress 3 to Exit\nEnter your choice here: ");
scanf("%d",&choice);
printf(" ");
//case 1 is decryption
switch(choice){
case 1:
for(x=1;x<=25;x++){//loop to shift the character
for(f=0;(f<=400 && code[f]!=0);f++) // for loop to print out all the possible solutions
{ //thie two loop with ouput all 25 result and store in an array
e=0,a=0,r=0,i=0,o=0;
if(code[f]==32){//32 means space in ascii code
store[x][f]=32;
continue;//if there is a space it will continue
}
if(code[f]>=65 && code[f]<=90){
if(code[f]+x>90){
store[x][f]=((code[f]+x)-90)+64;
continue;
}
}
if(code[f]+x>122 && code[f]>96){
after[f]=((code[f]+x)-122)+96;
}
else{
store[x][f]=code[f]+x;
}
}
store[x][f] = '\0';
printf("After decrypting:%s\n", store[x]);
printf("%s\n",code);
}
for(x=1;x<=25;x++){//this is where i have problems
for(f=0;(f<=400 && code[f]!=0);f++){ // for loop to start finding the words
if(store[x][f]=='E'){
e++;
}
if(store[x][f]=='A'){
a++;
}
if(store[x][f]=='R'){
r++;
}
if(store[x][f]=='I'){
i++;
}
if(store[x][f]=='O'){
o++;
}
}
if(se<e){
se=e;
ce=x;
}
if(sa<a){
sa=a;
ca=x;
}
if(sr<r){
sr=r;
cr=x;
}
if(si<i){
si=i;
ci=x;
}
if(so<o){
so=o;
co=x;
}
}
printf("%d %d %d %d %d\n",ce,ca,cr,ci,co);
printf("The program thinks the possible solution are:\n%s\n%s\n%s\n%s\n%s\n",store[ce],store[ca],store[cr],store[ci],store[co]);
break;
case 2:
break;
case 3:
printf("GOODBYE");
break;
default:
printf("Errors");
return 0;
}
}
Aucun commentaire:
Enregistrer un commentaire