I made this really strange and not working code just to count the total number of letters in a string(user inputs then stops with enter), as well as divide each individual character(a, b, c etc) by the total number and show that as well. The reasoning seems right but can someone give me an idea where i'm writing the wrong code? this is my first code in c.
thanks a lot
lol sorry I guess you can delete it then :P
/*Header file declarations. You may not include any additional header files*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 200000
#define NUM_LETTERS 26
/* Global variable Declarations. You may not include any additional global variables */
int cntLetters[NUM_LETTERS], totalLetters;
char cipherText[BUFSIZE];
/* You may not modify the table[] global variable */
const float table[] = {0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, /* letter frequency table */
0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406, 0.06749, 0.07507, 0.01929,
0.00095, 0.05987, 0.06327, 0.09056, 0.02758, 0.00978, 0.02360, 0.00150, 0.01974, 0.00074};
int estimatedKey;
float minimumError;
/* You may not modify the PrintMsg function */
void PrintMsg(int num) {
int i;
switch(num) {
case 0:
printf("Please enter cipher text: ");
fflush(stdout);
break;
case 1:
printf("Letter frequency table: \n");
for(i=0; i<NUM_LETTERS; i++)
printf("%c - %f\n", ('a'+i), ((float)cntLetters[i])/((float)totalLetters));
break;
case 2:
printf("Estimated key = %d\n", estimatedKey);
break;
case 3: printf("Estimation error = %f\n", minimumError);
break;
case 4: printf("Plain text: \n");
break;
default:
fprintf(stderr, "Error - unknown message number.\n");
break;
}/* switch */
}/* function PrintMsg */
/* You may not modify the calcError function */
float calcError(int offset) {
int i;
float accum=0.0, temp;
for(i=0; i<NUM_LETTERS; i++) {
temp = ((float)cntLetters[i] / (float) totalLetters) - table[(i+(NUM_LETTERS-offset))%NUM_LETTERS];
accum += temp*temp;
}/* for */
return accum;
}/* function calcError */
void printPlainText() {
/* declare variables here */
/* Place functional code here */
}/* function printPlainText */
void Count() {
/* declare variables here */
int count_a;
int count_b;
int count_c;
int count_d;
int count_e;
int count_f;
int count_g;
int count_h;
int count_i;
int count_j;
int count_k;
int count_l;
int count_m;
int count_n;
int count_o;
int count_p;
int count_q;
int count_r;
int count_s;
int count_t;
int count_u;
int count_v;
int count_w;
int count_x;
int count_y;
int count_z;
/* Place functional code here */
getchar()= char;
switch (char) {
case 'a': count_a++; break;
case 'b': count_b++; break;
case 'c': count_c++; break;
case 'd': count_d++; break;
case 'e': count_e++; break;
case 'f': count_f++; break;
case 'g': count_g++; break;
case 'h': count_h++; break;
case 'i': count_i++; break;
case 'j': count_j++; break;
case 'k': count_k++; break;
case 'l': count_l++; break;
case 'm': count_m++; break;
case 'n': count_n++; break;
case 'o': count_o++; break;
case 'p': count_p++; break;
case 'q': count_q++; break;
case 'r': count_r++; break;
case 's': count_s++; break;
case 't': count_t++; break;
case 'u': count_u++; break;
case 'v': count_v++; break;
case 'w': count_w++; break;
case 'x': count_x++; break;
case 'y': count_y++; break;
case 'z': count_z++; break;
}
totalLetters= count_a+count_b+count_c+count_d+count_e+count_f+count_g+count_h+count_i+count_j+count_k+count_l+count_m+count_n+count_o+count_p+count_q+count_r+count_s+count_t+count_u+count_v+count_w+count_x+count_y+count_z;
}/* function Count */
int main(int argc, char *argv[]) {
/* declare variables here */
int char;
/* perform initialization here */
totalLetters=0;
/* Place functional code here */
PrintMsg(0);
do {(getchar() = char);
void Count(char);
PrintMsg(1);}
while (char != '\n' || char != 'Enter');
return 0;
}/* function main */