Hey all,

I have an array of uppercase letters, mixed with other non-alphaitalic charcters. I need to keep a frequency of the each of the uppercase letters, and how many other characters entered.

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.

I already wrote the code to change the array entered to uppercase. I don't know how to go about with the frequency count.

#include <iostream>

using namespace std;

#include <stdio.h>

const char DECLARED_SIZE = 96;

void uppercase(char letters[]);

const char DELIMITER = '#';

int main()
{
cout << "ENTER UPTO 96 CHARACTERS, FOLLOWED BY A '#':\n";
char letters[DECLARED_SIZE], next;
int index = 0;
int count = 0;

for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
index = 0;
cin >> next;

while ((next != DELIMITER) && (index < DECLARED_SIZE))
{
letters[index] = next;
index++;
cin >> next;
}
letters[index] = next;
int num_used = index;
cout << "YOU ENTERED: \n";
for (index = 0; index < num_used; index++)
cout << letters[index] << " ";
cout <<endl;
uppercase(letters);

return 0;
}

//CHANGE LOWERCASE TO UPPERCASE.

void uppercase(char letters[])
{
int size = 0;
int k = 0;
for (k = 0; letters[k] != DELIMITER; k++)
{
size++;
}
for (k = 0; k < size; k++)
{
if(letters[k] >= 'a' && letters[k] <= 'z')
{
letters[k] -= 32;
}
else
{
letters[k] = letters[k];
}
}
return;
}

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.

This is all I can do, I'm sorry.

char others = 0;
int counter = {0};
cin >> letters[k]; //from above code//

I haven't learnt how to loop through an array, or how to use the isupper() function.

Okay, the thing is that moderataors arent allowed to post code to help people out, its against the rules so I will just give you a short tutorial.

For learning about loops go here
http://www.cprogramming.com/tutorial/c/lesson3.html

For learning about accepting user input go here
http://www.cprogramming.com/tutorial/string.html

For use of toupper function go here
http://www.cprogramming.com/fod/toupper.html

Create a program from start with the help of these tutorials, adding feature one by one so that you dont mess it up, and then post your new program.

Thanks, and God Bless,
Jonn.

Ah... yes thank you "Father" :D

I did, and it crashed. I tried this:

char i;

for (i = 0 ; i < 255; i++)
{
if( count > 0)
printf("Char: %c was found %d times", count, count);

}

That's why I posted the question again, to see if anyone has another alternate code. The case, switch code is too long.

The problem is somewhere else. You should post the rest of your program. You should have declare array count like this:

``int count = {0}``

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.

``````for (int i=0; i<MAX_SIZE; i++)
{
switch (letters[i])
{
case 'a': counter++; break;
case 'b': counter++; break;
case 'c': counter++; break;
//etcetcetc

}
}``````

You could use:

``````for (int i=0; i<MAX_SIZE; i++)
{
counter[letters[i] - 48]++;
}``````

Thanks,

Should the bracket be there between counter and letters?

for (int i=0; i<MAX_SIZE; i++)
{
counter[letters - 48]++;
}

Yes, 2 opening and 2 closing brackets

Thanks niek e. I will try to implement the code tonight. I have a question though, why 48?

for (int i=0; i<MAX_SIZE; i++)
{
counter[letters - 48]++;
}

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'.

Ah... tricky, thanks. Then

cout << letters <<"appears "<< counter "times\n";

to see each character.

Ah... tricky, thanks. Then
to see each character.

Hmm... no its incorrect. Correct one is:

``cout << letters[i] <<" appears "<< counter[i] << " times\n";``

Hmm... no its incorrect. Correct one is:

``````cout << letters[i] <<" appears "<< counter[i] << "
times\n";``````

I did as the doctor ordered, the program ran, but did not calculate the count. Here's my code:

``````#include <iostream>
using namespace std;
#include <stdio.h>
const char DECLARED_SIZE = 96;
void uppercase(char letters[]);
void frequency(char count[]);
const long LIMIT = 96;
const char DELIMITER = '#';

int main()
{
cout << "ENTER UPTO 96 CHARACTERS, FOLLOWED BY A '#':\n";
char letters[DECLARED_SIZE], next;
int index = 0;
int count = 0;

for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
index = 0;
cin >> next;

while ((next != DELIMITER) && (index < DECLARED_SIZE))
{
letters[index] = next;
index++;
cin >> next;
}
letters[index] = next;
int num_used = index;
cout << "YOU ENTERED: \n";
for (index = 0; index < num_used; index++)
cout << letters[index] << " ";
cout <<endl;
uppercase(letters);

return 0;
}
//CHANGE LOWERCASE TO UPPERCASE.
void uppercase(char letters[])
{
int size = 0;
int k = 0;
for (k = 0; letters[k] != DELIMITER; k++)
{
size++;
}
answer = new char[size + 1];
for (k = 0; k < size; k++)
{
if(letters[k] >= 'a' && letters[k] <= 'z')
{
letters[k] -= 32;
}
else
{
letters[k] = letters[k];
}

int counter[] = {0};
for (int i = 0; i < 255; i++)
{
counter[letters[i] - 48]++;
cout << "LETTER - " << letters[i] << " APPEARS " << counter[i] << "TIMES\n";
}

}
for (k = 0; k < size; k++)
{
if (letters[k] >= 'A' && letters[k] <= 'Y')

{
}
else if (letters[k] == 'Z')
{
}
else if (letters[k] < 'A' || letters[k] > 'Z')
{
}

cout << answer[k] << " ";

}
return;
}``````
``````int counter[] = {0};
for (int i = 0; i < 255; i++)
{
counter[letters[i] - 48]++;
cout << "LETTER - " << letters[i] << " APPEARS " << counter[i] << "TIMES\n";
}``````

array counter is not declared correctly. You have to tell it how many integers are in the array by putting a number between those two square bracket thingies.

``int counter = {0}``

the loop counter should not go up to 255 because the loop counter is used as an index into character array letters and that array has at most 96 letters (could be fewer). So the loop counter must count from 0 to the number of characters in letters array.

``````<snip>
else
{
letters[k] = letters[k];
}``````

Above from function uppercase(). That is totally unnecessary, it is a do-nothing statement. You should just delete it (your compiler probably will delete it anyway).

``````for (k = 0; k < size; k++)
{
if (letters[k] >= 'A' && letters[k] <= 'Y')

{
}
else if (letters[k] == 'Z')
{
}
else if (letters[k] < 'A' || letters[k] > 'Z')
{
}``````

What is all that? First, letters array has already been converted to all upper-case, so there is no need to test for it here. Second, what is the purpose of array answer. If that was just for your own debugging you should have deleted it before posting -- and you will most certainly have to delete it before turning it in as your assignment.

Oh, the purpose of that is to transpose the letters, A to B, B to C, .... Z to A. The instructor asked for that. That took me like 8hrs to do, I forgot that ''=" is not the same as "==". lol.

So I hope your problem is solved ?
Can I mark this thread as solved ?

Hey SOS, nice to see you again.

Not yet, I will try the code tonight. I don't have C++ at work. You can close the other 2 threads though.

Frequency of characters entered, and I need help with C++ project.

Even i am happy seeing you in action.

Just wanted to let you know the one who starts the thread can mark the thread by clicking on the link "mark as solved" which lies at the left hand top side of your web page.

It didn't work SOS. And it's due today at 1700hrs.

Please dont be vague. It doesnt work, doesnt convey us anything. State what kind of output were you expceting and what have you got.

BTW did you take a look at the prev post of Mr. Dragon, he pointed out some problem in your code, did you get it corrected ?

``````#include <iostream>
using namespace std;
#include <stdio.h>
const char DECLARED_SIZE = 96;
void uppercase(char letters[]);
void frequency(char count[]);
const long LIMIT = 96;
const char DELIMITER = '#';
int main()
{
cout << "ENTER UPTO 96 CHARACTERS, FOLLOWED BY A '#':\n";
char letters[DECLARED_SIZE], next;
int index = 0;
int count = 0;

for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
index = 0;
cin >> next;

while ((next != DELIMITER) && (index < DECLARED_SIZE))
{
letters[index] = next;
index++;
cin >> next;
}
letters[index] = next;
int num_used = index;
cout << "YOU ENTERED: \n";
for (index = 0; index < num_used; index++)
cout << letters[index] << " ";
cout <<endl;
uppercase(letters);

return 0;
}
//CHANGE LOWERCASE TO UPPERCASE.
void uppercase(char letters[])
{
int size = 0;
int k = 0;
for (k = 0; letters[k] != DELIMITER; k++)
{
size++;
}
answer = new char[size + 1];
for (k = 0; k < size; k++)
{
if(letters[k] >= 'a' && letters[k] <= 'z')
{
letters[k] -= 32;
}
else
{
letters[k] = letters[k];
}

int counter = {0};
for (int i = 0; i < 255; i++)
{
counter[letters[i] - 48]++;
cout << "LETTER - " << letters[i] << " APPEARS " << counter[i] << "TIMES\n";
}
}
for (k = 0; k < size; k++)
{
if (letters[k] >= 'A' && letters[k] <= 'Y')

{
}
else if (letters[k] == 'Z')
{
}
else if (letters[k] < 'A' || letters[k] > 'Z')
{
}

cout << answer[k] << " ";

}
return;
}``````

He wanted me to initialize the array to 255, and I did. It runs 255 times with wrong output.
I'll post the code

The loss of indentation makes the code impossible to read and find the problem areas. It would be really better if you indented the code and resposted.

Some points:

1. WHy is the logic for counting the characters present in the function make uppercase? Why not create a seperate function for it?
2. Are you not allowed to use inbuilt functions ?