hi

i am trying to count the occurrences of different words in a string array.

while(end of array)
{
while(end of array)
{
compare ith element with all other elements
inc counter
}
increment the element number
}

but i guess something is terribly wrong here as i am not able to get the required result. can someone point me out where i could have gone wrong!!! thanks!!!!

I would recommend posting your code. You are not going to find anyone here that will give you answers without showing your effort.

char *stringarray[] = { "mama", "tata", "bunica", "mama", "tata", "georgel" };
	map< char*, int > mycountingmap; 
	
	for ( size_t i = 0; i < sizeof( stringarray ) / sizeof( char* ); ++i )
		++mycountingmap[ stringarray[i] ] ;

       //print result
       for ( size_t i = 0; i <  sizeof( stringarray ) / sizeof( char* ); ++i )
		cout<<stringarray[i] <<" "<< mycountingmap[ stringarray[i] ] <<" times"<<endl;

At first you must load map contents (where is the code? ). But you have another problem: std::map<char*,int> can't help you because its key_type is char* but operator< for char* type compares pointer values, not referred C-strings. Therefore you will get wrong dictionary...

It's possible to define a simple wrapper class for char* type with its own operator< but it seems you can't do it now. So consider map<string,int> type for your dictionary: it's less effective but a very simple solution.

Now get F1 help on map insertion (or google it) then send us the next version of the program product...

At first you must load map contents (where is the code? ). But you have another problem: std::map<char*,int> can't help you because its key_type is char* but operator< for char* type compares pointer values, not referred C-strings. Therefore you will get wrong dictionary...

It's possible to define a simple wrapper class for char* type with its own operator< but it seems you can't do it now. So consider map<string,int> type for your dictionary: it's less effective but a very simple solution.

Now get F1 help on map insertion (or google it) then send us the next version of the program product...

map<char*,int> is ok for he's requirements. He just has to count them, not have them in a sorted dictionary

To kux

map<char*,int> is ok for he's requirements. He just has to count them, not have them in a sorted dictionary

Alas, all four text literals "mama" (for example) have different addresses (as usually). So map<char*,int> contains four different elements for the word "mama"! Obviously, it's not ok for his/her requirements. Read the original post carefully:

i am trying to count the occurrences of different words in a string array.

;)

To kux

Alas, all four text literals "mama" (for example) have different addresses (as usually). So map<char*,int> contains four different elements for the word "mama"! Obviously, it's not ok for his/her requirements. Read the original post carefully:

;)

hmm, i think you are wrong here. I mean i'm shure you are wrong here. On my STL implementation std::map has a template specialization that for char* keys uses the char* content to identify keys, not the address. Just try it. It will corectly count the words...

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.