Hmm.. ok I will give you a simple algo, you attempt it and post your code if you get stuck:Create a variable character count which counts the non uppercase chars and initialize it to zero.
Create an array of 26 integers and initialize it to zero. This array will keep track of the uppercase alphabet count.
Accept the input ( uppercase and other characters ) from the user into a array of characters ( C style string ).
Loop through the character array from the start ( i.e. position 0 ) till the end of string is encountered ( '\0' ).
For each character use the isupper( ) function to check whether the character is upper case.
If yes then increment the value corresponding to that character in the array. (Hint characters are actually integer values, so manipulate them to obtain the correct array index.
IF the char is not uppercase then just increment the count var.
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Did you read the algo I posted above? It exactly describes how you should go about finding freqency count.
And btw is there any restriction on using in built functions in your assignment, coz there is a function toupper( ) which accepts a character and returns its uppercase. So you should use that function instead of writing your own.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Ah... yes thank you "Father" :D
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Paradox: didn't you read this thread that you started a couple days ago ?? The question was answered there, complete with examples.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
The problem is somewhere else. You should post the rest of your program. You should have declare array count like this:
int count[255] = {0}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I did, and it crashed. I tried this:
That's strange because I tested dragon's code from the other post and it works fine for me...
As far as the switch-case you're right, it's to many lines.
Instead of
for (int i=0; i<MAX_SIZE; i++)
{
switch (letters[i])
{
case 'a': counter[0]++; break;
case 'b': counter[1]++; break;
case 'c': counter[2]++; break;
//etcetcetc
}
}
You could use:
for (int i=0; i<MAX_SIZE; i++)
{
counter[letters[i] - 48]++;
}
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Yes, 2 opening and 2 closing brackets
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Since 48 is the ASCII value of character '0'.
It would be better if you replace 48 in your formula to '0' since it would make more sense and you dont have to remember all the integer equivalents of the different characters. Just using random literals in teh program makes it difficult to understand for others and even for you after some time has elasped.
In short just mysterious literals should be avoided, follow my method and use '0'.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Ah... tricky, thanks. Then
to see each character.
Hmm... no its incorrect. Correct one is:
cout << letters[i] <<" appears "<< counter[i] << " times\n";
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734