I need to read in sentence and then put each letter into an arraylist. When I'm done I need to out put the letter that were in the sentence and how many times they appeared.
ex:
hello how are you
h2
e2
l2
o3
w1
r1
y1
u1

So, I need to read in a string send the first letter, h, to an arraylist. Then when it gets to e, check the arraylist to see if e already exists, since it doesn't I send it to a different position in the arraylist. When it gets to the first l it will send it ot the arraylist. when it gets to the second l it will check and find the l. I getEntry increment the count to 2 and then setEntry. I understand the logic, not the implementation. Here is what I have.

#include "ArrayList.h"
#include <iostream>
#include <string>

struct ItemType
{
  char letter;
  int count;
};

int main()
{
    ArrayList<ItemType> mylist;
    ItemType anItem;
    string sentence;


    cout << "Please enter a sentence: "<< endl;
    getline(cin,sentence);

    for (int i= 0; i<sentence.length(); i++)
        {
            anItem.letter = sentence[i];
            anItem.count ++;
        }

    mylist.insert(1,anItem);

    for (int i=0; i<mylist.getLength(); i++)
        {
            ItemType anItem = mylist.getEntry(i+1);
            cout << anItem.letter << anItem.count << endl;
        }   


return 0;
}

If i could get some help, any type of little tips, that's be great.

Recommended Answers

All 3 Replies

I will use associative array or any think like a hash.
read in the string and insert it into the object.
pseudo

user dataType<string,int> foo;
string data;
getline(cin,data);

loop through;
for(int c =0;c< data.lenght();c++){
//now yu check to see if the letter has been entered before.
//in this case string is the key and int is the value.
if(foo.key(c)==c){
int v= foo.getValue(c);
foo[c]=v+1;
}
else{
foo[c]=1;
}

}

Got the idea??? simple

How do insert each charachter and count into an array position?

I know it's something like this mylist.insert(1,anItem); but when I do this in a loop I get a cumulative count for the whole word instead of each letter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.