///letter count/// c++

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

Need help in C++ coding a program which determines most frequently used letters in the text and least frequently. * should count both upper & lower case as same character * ignore non-alphabetic char (space,.,apostrophe,etc) * if there is a tie for the most common letter, program only needs to display one of the letters * only c++ string class used * program must include one function that uses 1 or more reference variable as param * no global variable may be used Example output: The most common letter is E with 147 occurrences. The least common letter is J with 0 occurrences.

cp21 · Sep 15, 2015 5:39 PM · 4,154 views

11 Replies

Probably create a hash or like a dictionary that maps characters to their count. Bye and the max of the count. I would write a quick method in c but I got my own stuff to do, things are getting pretty intense for me right now.

kb3292 · Sep 16, 2015 1:54 AM

Its all Gravy kb3292... Any1 else care to share or have done similar project

cp21 · Sep 16, 2015 8:11 AM

what is the input ? what is "the text" ? Last edited: 16-Sep-15 05:58 PM

datamap · Sep 16, 2015 5:57 PM

To be, or not to be--that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune Or to take arms against a sea of troubles And by opposing end them. To die, to sleep-- No more--and by a sleep to say we end The heartache, and the thousand natural shocks That flesh is heir to. 'Tis a consummation Devoutly to be wished. To die, to sleep-- To sleep--perchance to dream: ay, there's the rub, For in that sleep of death what dreams may come When we have shuffled off this mortal coil, Must give us pause. There's the respect That makes calamity of so long life. For who would bear the whips and scorns of time, Th' oppressor's wrong, the proud man's contumely The pangs of despised love, the law's delay, The insolence of office, and the spurns That patient merit of th' unworthy takes, When he himself might his quietus make With a bare bodkin? Who would fardels bear, To grunt and sweat under a weary life, But that the dread of something after death, The undiscovered country, from whose bourn No traveller returns, puzzles the will, And makes us rather bear those ills we have Than fly to others that we know not of? Thus conscience does make cowards of us all, And thus the native hue of resolution Is sicklied o'er with the pale cast of thought, And enterprise of great pitch and moment With this regard their currents turn awry And lose the name of action. -- Soft you now, The fair Ophelia! -- Nymph, in thy orisons Be all my sins remembered.

cp21 · Sep 16, 2015 6:16 PM

I can tell you a dumb way to solve this ... I have to think more to find better way ... 1. Read the lines and store as a string 2. Using regular expression save only alphabetic 3. C++ should have something like ToUpper() or ToLower() use it to eliminate case sensitivity 4. write a method where it takes a char or string and return an integer ( what this should do is remove all the characters except the one that is passed) for example if a is passed the string becomes "aaaaaaaaaaaaaaa" that will give all the a's used in that text. count it and return it. this will give the frequency. 5. call this method 26 times with every alphabet 6. Find the highest count and find the lowest count Last edited: 16-Sep-15 07:04 PM Last edited: 16-Sep-15 07:07 PM

datamap · Sep 16, 2015 7:02 PM

Code please, due next week

cp21 · Sep 17, 2015 11:36 AM

Here's a naive implementation in c++ (used it after a long time and can be definitely improved). FWIW. Also figure out the indentation thing. Also test!! // countletters.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include using namespace std; void someFunc(int a); class LetterCounter { public: LetterCounter(); void SomeMethod(int a); void ReadFile(char *fileName); void ParseTextAndCountLetters(); void getMinMaxChar(char *minChar, char *maxChar, int *minCount, int *maxCount); ~LetterCounter(); private: FILE *fpt; char line[15]; int frequency[26]; string stext; }; LetterCounter::~LetterCounter() { // empty destructor // } LetterCounter::LetterCounter() { for (int i = 0; i 64 && c *maxCount) { *maxCount = frequency[i]; *maxChar = i + 65; } if (frequency[i] < *minCount) { *minCount = frequency[i]; *minChar = i + 65; } } } int main() { LetterCounter lc; char *fullFileName = "C:\\Users\\kundale\\Documents\\Visual Studio 2015\\Projects\\countletters\\Debug\\sometext.txt"; char minChar, maxChar; int minCount, maxCount; minChar = maxChar = minCount = maxCount = 0; printf("Hello World!"); someFunc(1); lc.SomeMethod(1); lc.ReadFile("something"); lc.ReadFile(fullFileName); lc.ParseTextAndCountLetters(); lc.getMinMaxChar(&minChar, &maxChar, &minCount, &maxCount); printf("\nminimum letter :: %c with count = %d", minChar, minCount); printf("\nmaximum letter :: %c with count = %d", maxChar, maxCount); printf("\nPress a key to exit!!"); getchar(); return 0; } void someFunc(int a) { printf("\n in a some function"); return; }

kundale · Sep 17, 2015 2:58 PM

bump still waiting for working code

cp21 · Sep 20, 2015 6:13 PM

bump

cp21 · Sep 21, 2015 1:15 PM

kata gayab ho c++ ko guru haru

cp21 · Sep 21, 2015 5:45 PM

hey bro can you help me with this project letter count i can pay you for your work

cp21 · Sep 21, 2015 6:19 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all