### Frequency of letters and numbers with an array

Hello, I'm trying to write a program that will check the frequency of amount of numbers, letters, and other keys in a txt document. I think I have the letters part, but is there a way to have it check for numbers, and symbols, in just one line of an array? As opposed to typing them all out? So total my array would just be 28. I'm not trying to check for frequency of all numbers and symbols on the keyboard. Just whether or not a number is there, or a symbol like = or + or whatever. So if there is 1,2,8,3,4 in the document, then there are five numbers total. Same for symbols. Letters though, I am trying to see how much each one shows up. I think I can have it check correctly once I get the array figured out.

 ``1234567891011`` ``````#include "stdafx.h" #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { char countLetter [28] = {'A','B','C','D','E',F,G,H,I, 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W', 'X','Y','Z'}; } ``````
I'm just a beginner but this sounded
close to what you are looking for.

http://www.cplusplus.com/reference/istream/istream/get/
suppose your input has type char array (or string), and is named `str`
your letter frequency array: `int letterFreq[26] = {0};`
your number count: `int numberCount = 0;`
your other chars count: `int punctCount = 0;`

loop through all `char c` in `str`:
if c is upper: `++letterFreq[c - 'A'];`
else if c is lower: `++letterFreq[c - 'a'];`
else if c is number: `++numberCount;`
else if c is other characters: `++punctCount;`

To determine whether c is upper or lower or number... see the table in http://www.cplusplus.com/reference/cctype/
so, for the symbols and numbers, how do i have an array check for that? The only array response I see is
your letter frequency array: int letterFreq[26] = {0};
I do have a letter array. I need help adding to that - digits and symbols.
Thanks
So, is there any way to have an array track digits and symbols? I have an array with 28 elements available. 26 for letters, one for digits, one for symbols. It's to track how many letters, digits, and symbols there are in a text document. Trying to see if it's possible.

Thanks
I guess this can't be done. I'll resolve it.
Hi, what exactly are you looking to do?

1. Check the frequency of one particular letter, number or symbol?
or...
2. Check the amount of letters, numbers or symbols within the text file?
Well, in my original post, I said I was trying to check the frequency of all numbers, letters, and other keys in a txt document, and I was trying to do that using only one array with 28 elements. 26 for letters, 1 for digits, 1 for other.
Last edited on
Ok, but when you say the frequency of ALL numbers, do you mean the collective of individual frequency of each number?

Edit:

Very simple example:

A 1 K L 8 T 3 6 3 2

In the above example, number of characters is 10, the frequency of the collective numbers is 6/10, the frequency of individual numbers is
1: 1/10
2: 1/10
3: 2/10
6: 1/10
8: 1/10

And the frequency of the collective letters is 4/10, each having an individual frequency of 1/10
Last edited on
If there is 1 digit, then the frequency would be 1. 2 digits, 2, and so on.
closed account (3qX21hU5)
Then just use the cctype header and the functions provided by it.

If it is a digit you can then add to the digit counter.
 ``1234`` `````` if (isdigit(Y)) { ++numberCounter }``````

If it is a symbol add to the symbol counter.

 ``1234`` ``````if (ispunct(Y)) { ++symbolCounter }``````

yah, I could, but I'm searching for a way to do that in a 28 element array - 26 characters, 1 digit, 1 other.
my original post -
cheshirecat (72) Nov 24, 2012 at 10:17pm
Hello, I'm trying to write a program that will check the frequency of amount of numbers, letters, and other keys in a txt document. I think I have the letters part, but is there a way to have it check for numbers, and symbols, in just one line of an array? As opposed to typing them all out? So total my array would just be 28. I'm not trying to check for frequency of all numbers and symbols on the keyboard. Just whether or not a number is there, or a symbol like = or + or whatever. So if there is 1,2,8,3,4 in the document, then there are five numbers total. Same for symbols. Letters though, I am trying to see how much each one shows up. I think I can have it check correctly once I get the array figured out.
I guess the labels on the array don't matter, so I could just put array [28] = {'A','B','C','D','E',F,G,H,I,'J','K','L','M','N','O','P','Q','R','S','T','U','V','W',
'X','Y','Z','Digit','Other'} I guess. I suppose I need to figure out how array frequency works before I can get this answered the way I want.
Here is a full solution. I rushed it so there may be a bug with it but I gave it a quick test and it seemed to work.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110`` ``````#include #include using namespace std; /* * Relevant ASCI Codes (src: http://www.asciitable.com/ ) * * This are the ranges of ASCI codes for characters. For example, the lower case letter, a, has an asci code of 97. This means that * char ch = 97; * char ch = 'a'; * Are the same thing. This is useful for testing ranges of characters. The rectangular brackets imply that it includes that value. This means that [a, b] is the range, between values a and b, inclusively. * * Letters: * upper: [65, 90] * lower: [97, 122] * * Numbers: * [48, 57] * * Symbols * 1. [33, 47] * 2. [58, 64] * 3. [91, 96] * 4. [123, 126] * */ bool isLetter(char ch); bool isNumber(char ch); bool isSymbol(char ch); int main() { unsigned int totalLetterCount = 0; unsigned int totalNumberCount = 0; unsigned int totalSymbolCount = 0; unsigned int totalCharCount = 0; string buffer; cout << "The file must be located within the same directory as the binary. " << endl; cout << "What is the file name? "; cin >> buffer; ifstream input(buffer.c_str()); if( !input.is_open() ) // Test if the file is opened { cout << "Failed to open file: " << buffer << endl; return 1; } else { while(input.good()) // http://cplusplus.com/reference/ios/ios/good/ { getline(input, buffer); // Here we read a line from the file into the string, buffer for(unsigned short i = 0; i < buffer.size(); i++) // This iterates through each character on the line, to test if its a letter, number, or symbol { totalCharCount++; if( isLetter(buffer[i]) ) // Is it a letter? totalLetterCount++; if( isNumber(buffer[i]) ) // Is it a number? totalNumberCount++; if( isSymbol(buffer[i]) ) // Is it a symbol? totalSymbolCount++; } } } cout << "There are..." << endl; cout << totalLetterCount << " letters. " << endl; cout << totalNumberCount << " numbers. " << endl; cout << totalSymbolCount << " symbols. " << endl; cout << "And a total of " << totalCharCount << " characters. " << endl; return 0; } bool isLetter(char ch) { if(ch >= 65 && ch <= 90) return true; if(ch >= 97 && ch <= 122) return true; return false; } bool isNumber(char ch) { if(ch >= 48 && ch <= 57) return true; return false; } bool isSymbol(char ch) { if(ch >= 33 && ch <= 47) return true; if(ch >= 58 && ch <= 64) return true; if(ch >= 91 && ch <= 96) return true; if(ch >= 123 && ch <= 126) return true; return false; }``````