Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,446 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,219 people online right now. Registration is fast and FREE... Join Now!




Program To Count and Display The Number of Letter Occerences

 
Reply to this topicStart new topic

Program To Count and Display The Number of Letter Occerences

mastershredder
20 Nov, 2007 - 02:59 PM
Post #1

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 17


My Contributions
I need this program to count a sentence string and display A-Z the amount of times that individual letter has occured. I have been workin on this for a few days now and am stuck, if anyone could help id really appreciate it! Thanks

CODE
#include <iostream>

int prompt(void);
int count_let(void);
int display(void);

using namespace std;


char ch;
int rc;
int 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;
int count;
int acount = A;
int bcount = B;
int ccount = C;
int dcount = D;
int ecount = E;
int fcount = F;
int gcount = G;
int hcount = H;
int icount = I;
int jcount = J;
int kcount = K;
int lcount = L;
int mcount = M;
int ncount = N;
int ocount = O;
int pcount = P;
int qcount = Q;
int rcount = R;
int scount = S;
int tcount = T;
int ucount = U;
int vcount = V;
int wcount = W;
int xcount = X;
int ycount = Y;
int zcount = Z;


int main()
{
    rc = prompt();
    rc = count_let();
    rc = display();
    return rc;
}


int prompt(void)
{
    cout << "Enter a string" << endl;
    cin >> ch;

    return 0;
}


int count_let(void)
{

    char letter[26] = {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};

    
    ch = cin.get();

    for(i=0; letter[26];ch++)
{
    if(letter[26]==ch)
    {
    acount++;
    bcount++;
    }
}
    
    switch(ch)
        {
        case 'a' :
        case 'A' : letter[0];acount++; break;
        case 'b' :
        case 'B' : letter[1];bcount++; break;
        case 'c' :
        case 'C' : letter[2];ccount++; break;
        case 'd' :
        case 'D' : letter[3];dcount++; break;    
        case 'e' :
        case 'E' : letter[4];ecount++; break;    
        case 'f' :
        case 'F' : letter[5];fcount++; break;    
        case 'g' :
        case 'G' : letter[6];gcount++; break;    
        case 'h' :
        case 'H' : letter[7];hcount++; break;    
        case 'i' :
        case 'I' : letter[8];icount++; break;    
        case 'j' :
        case 'J' : letter[9];jcount++; break;    
        case 'k' :
        case 'K' : letter[10];kcount++; break;    
        case 'l' :
        case 'L' : letter[11];lcount++; break;    
        case 'm' :
        case 'M' : letter[12];mcount++; break;    
        case 'n' :
        case 'N' : letter[13];ncount++; break;    
        case 'o' :
        case 'O' : letter[14];ocount++; break;    
        case 'p' :
        case 'P' : letter[15];pcount++; break;    
        case 'q' :
        case 'Q' : letter[16];qcount++; break;    
        case 'r' :
        case 'R' : letter[17];rcount++; break;    
        case 's' :
        case 'S' : letter[18];scount++; break;
        case 't' :
        case 'T' : letter[19];tcount++; break;    
        case 'u' :
        case 'U' : letter[20];ucount++; break;    
        case 'v' :
        case 'V' : letter[21];vcount++; break;
        case 'w' :
        case 'W' : letter[22];wcount++; break;    
        case 'x' :
        case 'X' : letter[23];xcount++; break;    
        case 'y' :
        case 'Y' : letter[24];ycount++; break;    
        case 'z' :
        case 'Z' : letter[25];zcount++; break;    
        }
      
    return 0;
}

int display(void)
{
    cout << "A Occurred " << acount << " times " << endl;
    cout << "B Occurred " << bcount << " times " << endl;
    cout << "C Occurred " << ccount << " times " << endl;

    return 0;
}


MOD EDIT: code.gif
Please Read the Forum Rules
Vulgar/Offensive Language will not be tolerated.

User is offlineProfile CardPM
+Quote Post

skaoth
RE: Program To Count And Display The Number Of Letter Occerences
21 Nov, 2007 - 01:28 AM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 356



Thanked: 12 times
Dream Kudos: 100
My Contributions
The way you are currently doing this is just too painful.

I noticed you are using STL and don't know if you are allowed to use
any of the data structures there. If you can this problem becomes
trivial by using the std::map<char, int> data structure.

Now for you code,

This looks like a problem. ch is defined as a char. So anytime you
read in your input, you are only reading 1 character. Either use a std::string
or create a char buffer
CODE

// From prompt(void)
cin >> ch;

// Define like this instead
char buffer[256];

// or define like this
std::string buffer2;


Next count_let()

CODE

    for(i=0; letter[26];ch++)
{
    if(letter[26]==ch)
    {
    acount++;
    bcount++;
    }
}


I'm not sure what you are trying to do here.
The for loop needs a terminal case.
letter[26] == Z and Z is undefined. It was never initialized to anything
so it should be treated as having a garbage value.

Ok, so how do we move towards a solution. Well think about what it is your trying to count.
In this case letters A-Z and a-z. If we look at the ASCII table we can find out that those
letters are really just the integers such that

A-Z == 65 to 90
a-z == 97 to 122

there are a couple of other values between Z and a. ( we don't care about those)
So what we need is a way to map the letters to a counter variable in this case we need
122 - 65 place holders(57)

That can be done like this
CODE

#define BINS 58

int counters[BINS] = {0};


Next we need to figure out how to map a letter (A-Z, a-z) to one of the bins in counters.
so think about it like this

counters[0] == A
counters[1] == B
counters[2] == C
....
....
counters[32] == a
counters[33] == b
...
...
counters[57] == z


NOTE: This is not assigning anything. This is simply a logical mapping between the
ASCII letters and the counters array.

The code below is the fix for count_let(). It will be up to you to finish/fix the other functions
CODE

std::string data;    // input string
int letters[58] = {0};

int count_let(void)
{
    char ch;
    for(int i=0; i < data.length(); i++)
    {
    ch = data[i];
        
        // why 65? Because 'A' == 65 and we want
        // letters[0] to map to 'A'
    letters[ch - 65]++;  
    }
}


All that remains is to fix your display function.

Cheers

This post has been edited by skaoth: 21 Nov, 2007 - 01:30 AM
User is offlineProfile CardPM
+Quote Post

mastershredder
RE: Program To Count And Display The Number Of Letter Occerences
21 Nov, 2007 - 09:38 AM
Post #3

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 17


My Contributions
ok i messed around with it and compiled it no problem, now i guess i have some (display) syntax issues because I cant get it to display the letter count occurence in the (display) function.


CODE
#include <iostream>
#define BINS 58

int prompt(void);
int count_let(void);
int display(void);

using namespace std;

char ch;
int rc;
int counters[BINS] = {0};
int letters[58] = {0};



int main()
{
    rc = prompt();
    rc = count_let();
    rc = display();
    return rc;
}


int prompt(void)
{
    cout << "Enter A Sentence" << endl;
    cin >> ch;
    cout << endl;

    return 0;
}

std::string data;    // input string

int count_let(void)
{
    char ch;
    for(int i=0; i < data.length(); i++)
    {
    ch = data[i];
        
        // why 65? Because 'A' == 65 and we want
        // letters[0] to map to 'A'
    letters[ch - 65]++;  
    }

    return 0;
}


int display(void)
{
    cout << "A Occurred " << letters[0]++ << " times " << endl;
    cout << "B Occurred " << letters[1]++ << " times " << endl;

    return 0;
}


Mod edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Program To Count And Display The Number Of Letter Occerences
21 Nov, 2007 - 09:43 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,115



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(mastershredder @ 21 Nov, 2007 - 11:38 AM) *

i guess i have some (display) syntax issues


You'll also have some display issues when you don't post using code tags.

code.gif


QUOTE(mastershredder @ 21 Nov, 2007 - 11:38 AM) *

CODE

int letters[58] = {0};

int display(void)
{
    cout << "A Occurred " << letters[0]++ << " times " << endl;
    cout << "B Occurred " << letters[1]++ << " times " << endl;

    return 0;
}



Why do you declare letters size to 58, & then only reference the 1st 2 array values?
User is online!Profile CardPM
+Quote Post

skaoth
RE: Program To Count And Display The Number Of Letter Occerences
21 Nov, 2007 - 10:23 AM
Post #5

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 356



Thanked: 12 times
Dream Kudos: 100
My Contributions
I apologize for the possible comfusing letters[] and counters[]
in my explaination.

To my defense I was writing that up at 1:30AM.

anyways letters[] and counters[] are supposed to be one and the same.

As far as displaying issues letters[] contains the counts of each occurance
of a letter.

You will need to loop through the array and display the count as no2pencil is
mentions

User is offlineProfile CardPM
+Quote Post

mastershredder
RE: Program To Count And Display The Number Of Letter Occerences
22 Nov, 2007 - 01:15 PM
Post #6

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 17


My Contributions
Ah, this is the best I can do, the code seems to make sense but every time i enter a string of letters i.e. "AAHDIIE" It shows straight 0's going down the board from the display function of everytime the letter occurred.










CODE
#include <iostream>

#define BINS 65

int print_title(void);
int prompt(void);
int count_let(void);
int display(void);

using namespace std;

char ch;
int rc = 0;
int counters[BINS] = {0};
int letters[65] = {0};



int main()
{
    
    rc = print_title();
    rc = prompt();
    rc = count_let();
    rc = display();
    
    return rc;
}

int print_title(void)
{
    cout << "Lab 7" << endl << endl;
    cout << endl;

    return 0;
}


int prompt(void)
{
    cout << "Enter A Sentence" << endl;
    cin >> ch;
    cout << endl;

    return 0;
}

std::string data;    // input string

int count_let(void)
{
    
    char ch;
    for(int i=0; i < data.length(); i++)
    {
    ch = data[i];
        
        // why 65? Because 'A' == 65 and we want
        // letters[0] to map to 'A'
    letters[ch - 65]++;  
    }

    return 0;

}


int display(void)
{
    cout << "A Occurred " << letters[0]++ << " times " << endl;
    cout << "B Occurred " << letters[1]++ << " times " << endl;
    cout << "C Occurred " << letters[2]++ << " times " << endl;
    cout << "D Occurred " << letters[3]++ << " times " << endl;
    cout << "E Occurred " << letters[4]++ << " times " << endl;
    cout << "F Occurred " << letters[5]++ << " times " << endl;
    cout << "G Occurred " << letters[6]++ << " times " << endl;
    cout << "H Occurred " << letters[7]++ << " times " << endl;
    cout << "I Occurred " << letters[8]++ << " times " << endl;
    cout << "J Occurred " << letters[9]++ << " times " << endl;
    cout << "K Occurred " << letters[10]++ << " times " << endl;
    cout << "L Occurred " << letters[11]++ << " times " << endl;
    cout << "M Occurred " << letters[12]++ << " times " << endl;
    cout << "N Occurred " << letters[13]++ << " times " << endl;
    cout << "O Occurred " << letters[14]++ << " times " << endl;
    cout << "P Occurred " << letters[15]++ << " times " << endl;
    cout << "Q Occurred " << letters[16]++ << " times " << endl;
    cout << "R Occurred " << letters[17]++ << " times " << endl;
    cout << "S Occurred " << letters[18]++ << " times " << endl;
    cout << "T Occurred " << letters[19]++ << " times " << endl;
    cout << "U Occurred " << letters[20]++ << " times " << endl;
    cout << "V Occurred " << letters[21]++ << " times " << endl;
    cout << "W Occurred " << letters[22]++ << " times " << endl;
    cout << "X Occurred " << letters[23]++ << " times " << endl;
    cout << "Y Occurred " << letters[24]++ << " times " << endl;
    cout << "Z Occurred " << letters[25]++ << " times " << endl;


    return 0;
}





User is offlineProfile CardPM
+Quote Post

illello
RE: Program To Count And Display The Number Of Letter Occerences
23 Nov, 2007 - 01:39 AM
Post #7

New D.I.C Head
*

Joined: 9 Sep, 2006
Posts: 7


My Contributions
First of all:

CODE

int prompt(void)
{
    cout << "Enter A Sentence" << endl;
    cin >> ch;
    cout << endl;

    return 0;
}


You are reading the char but you are not storing the value readed into any variable. Try to fix this one at first.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 12:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month