Frequency

This program will analyze a text file given by the user and report the frequency of letters within the file. The program is not case-sensitive, so all alphabetical characters will be counted and reported. A sample run could look something similar to the following.
Greetings!  My name is Brent and I will be analyzing a text file for you.      
Please enter the name of the file:  dictionary.txt
Here is a report on the frequency of each alphabetical character in the file "dictionary.txt".
Letter  Frequency
A          55    
B          31
...
Z           2 
Thank you for using this program.  Goodbye.



So here is what I currently have. It is able to open the file but I don't know where to go from here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * File: Letter Frequency
 * Author: Jake John
 * Course: CS I
 * Assignment: Letter Frequency
 * Due Date: March 8, 2021
 *
 * This program will analyze a text file given by the user and report 
 * the frequency of letters within the file.  The program is not 
 * case-sensitive, so all alphabetical characters will be counted and reported.
 */

#include <bits/stdc++.h>
using namespace std;

ifstream inFile;

int main(){
	
	cout << "Greetings! My name is Kolton Johnson and I will be analyzing"; 
	cout << " a text file for you." << endl;
	cout << "Please enter the name of the file: \dictionary.txt\" ";
	cout << "Here is a report on the frequency of each alphabetical character";
	cout << "in the file: \"dictionary.txt\"" << endl;
	
	inFile.open("dictionary.txt");
	if(!inFile){
		cout << "Error: Unable to open file.\n";
		exit(0);
	}
	
	
	
	return 0;
}
Last edited on
????
How exactly did you get that output with that program? In your cout statement, you have several things that don't jibe with your output. The name, for one.

But, to count the frequency of letters in a text file, you can read in each letter, check it with either a switch or an if/else if statement, and use a loop with an accumulator to count up the frequency.
I have this code for reference. It find a word from the same file but I don't know who to change it from reading the words and finding it to reading each word and telling how many letters in it and how many of each.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;

ifstream inFile;

string upCase(string strIn){
	string strOut;
	for(int s = 0; s < strIn.length(); s++){
		strOut += toupper(strIn[s]);
	}
	return strOut;
}

int main(){
	
	int wordCount;
	string word;
	string target;
	
	inFile.open("dictionary.txt");
	if(!inFile){
		cout << "Error: Unable to open file.\n";
		exit(0);
	}
	
	inFile >> wordCount;
	
	string strArray [wordCount];
	for(int w=0;w < wordCount; w++){
		inFile >> word;
		word = upCase(word);
		strArray[w] = word;
	}
	
	cout << "Enter a word: ";
	cin >> target;
	target = upCase(target);
	cout << "word as uppercase: " << target << endl;
	bool wordFound = false;
	for(int p = 0; p < wordCount; p++){
		if(target == strArray[p]){
			cout << "Word found at " << p << endl;
			wordFound = true;
			break;
		}
	}
	
	if(!wordFound){
		cout << "Word not found.\n";
	}
	
	return 0;
}
I have been working on this and this is what I got. But how do I get it to not read every single word from the file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;

int main()
{
   int freq[128];     // frequencies of letters
   ifstream inFile;   // input file
   char ch; 

   inFile.open("dictionary.txt");
   if (!inFile)
   {
      cout << "The input file could not be opened."<<endl;
      return 1;
   }

   // initialize frequency counts to zero for each possible letter
   for (int k = 0; k < 128; k++)
   {
      freq[k] = 0;
   }

   // Read the file, keeping track of frequency with which each letter occurs
   ch = inFile.get();
   while (ch != EOF)
   {
      cout << ch;
      ch = toupper(ch);
      freq[ch]++;
      ch = inFile.get();
   }
   // Print the output table
  cout << endl << "Letter frequencies in this file are as follows." << endl;
  for (char ch = 'A'; ch <= 'Z'; ch++)
  {
	  cout << ch << " : " << freq[ch] << endl;
  }
  return 0;
}
@jake john,
What do you mean? I thought you wanted to find the frequency of every letter in a text file? You can't do that without reading every word and analyzing each letter. Please, explain.

Also, why are you including <bits/stdc++.h>?? That is a nonstandard, platform-dependent library. There's no problem with using it, just be prepared for when you sell software to someone, and it crashes on them because they don't have that library.

Good luck,
max
>There's no problem with using it, just be prepared for when you sell software to someone, and it crashes on them because they don't have that library.
lolwut. It's not a matter of runtime/crashing, it just isn't a standard header and isn't portable. I agree it shouldn't be used.
Last edited on
Ok, crashing isn't the right word, but I think (hope) he got my meaning.
Topic archived. No new replies allowed.