Letter frequency counter
Dec 15, 2016 at 5:41pm UTC
I managed to write this with not much problem. I work in a school Supply store right now and people are always coming in needing to know how many packs of letters to buy to write a specific phrase.
Any other little coding projects you can suggest to kill some time?
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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
string phrase;
cout<<"Enter the phrase: " <<endl;
getline(cin, phrase);
int num[26];
int count;
char z;
for (int i=0;i<alpha.length();i++)
{
num[i]=0;
count=0;
cout<<alpha[i]<<" : " ;
for (int x=0;x<phrase.length();x++)
{
z=toupper(phrase[x]);
if (alpha[i]==z)
{
count++;
}
}
num[i]=count;
cout<<num[i]<<endl;
}
}
Dec 15, 2016 at 6:37pm UTC
You can try rewriting this using a map.
Dec 15, 2016 at 7:50pm UTC
Nice program, although it was not clear what line 15 was for at first.
Adding a comment there would be helpful if you want to come back and tweak it again, say in a year...
I tried to think of something useful in a school supply store, but so far I'm still thinking...
I hope you won't mind, I put your output lines together and changed it to only show the ones that are greater than zero. Hope you like the change, if not don't use it.
Seeing things like this others write and find useful is awesome, Thx for sharing.
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
#include <iostream>
#include <string>
// #include <cctype>
using namespace std;
int main()
{
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
string phrase;
cout<<"Enter the phrase: " <<endl;
getline(cin, phrase);
int num[26];
int count=0;
char z;
for (int i=0;i<alpha.length();i++)
{
num[i]=0;
count=0;
for (int x=0;x<phrase.length();x++)
{
z=toupper(phrase[x]);
if (alpha[i]==z)
{
count++;
}
}
if (count > 0)
{
cout<<alpha[i]<<" : " ;
num[i]=count;
cout<<num[i]<<endl;
}
}
}
Dec 16, 2016 at 1:51am UTC
... rewriting this using a map.
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
#include <iostream>
#include <locale>
#include <string>
#include <cctype>
#include <map>
using namespace std;
int main()
{
cout << "Enter phrase: \n" ;
string phrase;
getline (cin, phrase);
map<char , int > myMap;
locale loc;
for (auto & elem : phrase)
{
if (isalpha(elem, loc))
{
toupper(elem, loc);
myMap[toupper(elem)]++;
}
}
for (auto & elem : myMap)
{
cout << elem.first << " " << elem.second << '\n' ;
}
}
Dec 16, 2016 at 5:00am UTC
This is an adaptation of an earlier thread her which might be adaptable to the frequency counter problem:
http://www.cplusplus.com/forum/beginner/204508/#msg970600
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
#include <iostream>
#include <set>
int main ()
{
char mystring[] = "How many letters here 123?" ;
auto size = sizeof (mystring)/sizeof (char );
//SETUP MULTISET
std::multiset<char > mymultiset (mystring, mystring + size);
auto count = mymultiset.count(0);
// DISPLAY ARRAY CONTENTS
std::cout << " myints contains:" ;
for (auto i = 0; i < size; i++)
std::cout << ' ' << mystring[i];
std::cout << '\n' ;
// DISPLAY MULTISET CONTENTS
std::cout << "mymultiset contains:" ;
for (auto it = mymultiset.begin(); it != mymultiset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n' ;
// DISPLAY KEY vs COUNT FOR EACH ELEMENT
for (auto it = mymultiset.begin(); it != mymultiset.end(); ++it)
{
count = mymultiset.count(*it);
// KEYS WITH ODD COUNT
if (count % 2 == 1)
std::cout << " ODD Key: " << *it << " has " << count << " entries\n" ;
else
std::cout << "EVEN Key: " << *it << " has " << count << " entries\n" ;
for ( auto jump = 0; jump < count - 1 ; jump++)
it++;
}
std::cout << '\n' ;
return 0;
}
Last edited on Dec 16, 2016 at 5:02am UTC
Dec 17, 2016 at 4:12pm UTC
Thank you all for the suggestions! I'm not familiar with Map. I'll have to look that up.
Topic archived. No new replies allowed.