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 */

this so totally does not belong as a code snippet.

code snippets are supposed to be working code, that solves a particular problem, and that isl also be likely to be useful to at least a few other people in the world, somewhere.

but this ... this is a cry for help.

When you see code that so close to being exactly repetitive, it's time to look for better ways of doing this. ;)

int count_a;
int count_b;
int count_c;
int count_d;
int count_e;
//etc.

Just do a distribution count (aka bin sort), and be done with it. Use an int array of 26 elements, and subtract 'a' from all the char's. Now increment the appropriate element or "bin" in the array:
array[letter-'a']++;

and you're golden. If you want uppercase letters counted as well, then extend the array size, and subtract 'A' from the letter, instead.

And if you have a problem with your program, spell out EXACTLY what that problem is (won't compile, has warning, gives inaccurate results (and an example would be nice of the wrong input and output).

wow thanks for the cool help! I see what your're saying about subtracting the decimal value of a from the characters so we get like 1, 2, 3 etc for b, c, d, etc.. but I don't get the exact array thing.. like if i do that it'll increment the values inside array and thats all i need to do?
it gives a couple of errors which I'm not exactly sure about :
editedmp3.c: In function 'Count':
editedmp3.c:101: error: expected expression before 'char'
editedmp3.c:102: error: expected expression before 'char'
editedmp3.c: In function 'main:
editedmp3.c:139: error: two or more data types in declaration specifiers
editedmp3.c:139: warning: useless type name in empty declaration
editedmp3.c:146: error: expected expression before 'char'
editedmp3.c:147: error: conflicting types for 'Count'
editedmp3.c:71: error: previous definition of 'Count' was here
editedmp3.c:149: error: expected expression before 'char'
editedmp3.c:149:32: warning: character constant too long for its type

wow thanks for the cool help! I see what your're saying about subtracting the decimal value of a from the characters so we get like 1, 2, 3 etc for b, c, d, etc.. but I don't get the exact array thing.. like if i do that it'll increment the values inside array and thats all i need to do?
it gives a couple of errors which I'm not exactly sure about :
editedmp3.c: In function 'Count':
editedmp3.c:101: error: expected expression before 'char'

http://www.cplusplus.com/reference/clibrary/cstdio/getchar/

editedmp3.c:102: error: expected expression before 'char'
editedmp3.c: In function 'main:
editedmp3.c:139: error: two or more data types in declaration specifiers
editedmp3.c:139: warning: useless type name in empty declaration

Your line 139 is int char.....You cannot use char as a name of a variable as it is a keyword. Make it char_1 or something

editedmp3.c:146: error: expected expression before 'char'
The reason for this bug should be apparent now

editedmp3.c:147: error: conflicting types for 'Count'

I guess you wanted to call the function count here . But where as you have declared the function instead.

editedmp3.c:71: error: previous definition of 'Count' was here
editedmp3.c:149: error: expected expression before 'char'
editedmp3.c:149:32: warning: character constant too long for its type

Make the changes that I have suggested. Compile the code and post the results.
PS: Go through some of C programming tutorials

The count array should be of type int, and it's all very short and sweet.

int i;
int count[26] = {0];  //set all elements of count to 0
char string[54] = {"My favorite poem is The Rhyme of the Ancient Mariner"};

for(i = 0; i < 54; i++)
  count[string[i] - 'a']++;

printf("\n%d", count[0]);  //print out the number of a's (not A's).

I haven't run this code, so it might not be perfect, but it shows the idea as well as I can.

wow amazing now the only problem is with displaying the frequency of the letters it just keeps on looping instead of showing the values. Considering my short code for that (2) lines I'm not surprised but the thing is I'm calling the code(print msg 1) I don't know why its not displaying correctly!

Why don't you remove all that count_x++ stuff, and all those extraneous variables, and post up the latest version of your code.

That old stuff is hard to wade through.

If you guys bothered to look at the code you might have noticed in the original post:

/* Global variable Declarations. You may not include any additional global variables */
int cntLetters[NUM_LETTERS], totalLetters; 
char cipherText[BUFSIZE];

I wonder what cntLetters[NUM_LETTERS] is for?

i did bother to look at the original code. the first thing i noticed was:

/*Header file declarations. You may not include any additional header files*/

/* Global variable Declarations. You may not include any additional global variables */

/* You may not modify the table[] global variable */ 

/* You may not modify the PrintMsg function */

/* You may not modify the calcError function */

I don't know what to do here, because there's not much code left that we're allowed to modify.

i did bother to look at the original code. the first thing i noticed was:

/*Header file declarations. You may not include any additional header files*/

/* Global variable Declarations. You may not include any additional global variables */

/* You may not modify the table[] global variable */ 

/* You may not modify the PrintMsg function */

/* You may not modify the calcError function */

I don't know what to do here, because there's not much code left that we're allowed to modify.

You are kidding right? Looking beyond the 'defined' code and I also see

void printPlainText() { 
	/* declare variables here */
	/* Place functional code here */
}/* function printPlainText */

void Count() {
	/* declare variables here */
	/* Place functional code here */
}/* function Count */

int main(int argc, char *argv[]) {
	/* declare variables here */
	/* perform initialization here */
	/* Place functional code here */
}/* function main */

which is most of the program. Use the data values defined to write the rest of the program :icon_rolleyes:

commented: c'mon man, open your eyes: the OP is b.s.'ing us, he's trying to get us to do his homework for him. -1

i was being facetious. the comments are ridiculous, considering this is something that was originally presented as his own "Code Snippet"

by the way, i prefer "The Procrastinators Ten Commandments"

Procrastinators Ten Commandments

1.

:P

what i'm getting at, is the OP presented his "code snippet" as such:

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.

of course he "made this really strange and not working code" as his "first code in c"

complete with all the comments.

so, like, we should all totally get together and solve this really strange problem for him :icon_rolleyes:

.

commented: C'mon, it's obviously not a code snippet and hasn't been since someone corrected his misteak. After first mention is should not be harped upon. Mistake... -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.