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.
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;
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.
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.
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
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.
#include <iostream>
#include <fstream>
usingnamespace 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()
{
unsignedint totalLetterCount = 0;
unsignedint totalNumberCount = 0;
unsignedint totalSymbolCount = 0;
unsignedint 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(unsignedshort 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)
returntrue;
if(ch >= 97 && ch <= 122)
returntrue;
returnfalse;
}
bool isNumber(char ch)
{
if(ch >= 48 && ch <= 57)
returntrue;
returnfalse;
}
bool isSymbol(char ch)
{
if(ch >= 33 && ch <= 47)
returntrue;
if(ch >= 58 && ch <= 64)
returntrue;
if(ch >= 91 && ch <= 96)
returntrue;
if(ch >= 123 && ch <= 126)
returntrue;
returnfalse;
}