I am pretty new to programming. I am trying to write a code that counts the occurrence of a word, lists it and also the number of times it appears in a given text. This is what I have, but I can not get it to print the word once and count it if it occurs more than once. Please, any help would be appreciated!

// This program counts word occurence in a given text

#include <iostream>
#include <iomanip>

using namespace std;

class tokenizer
{
	char * src_ptr;
	char * c_token;
	char * token_start_ptr;
public:	
	tokenizer(const char * in_str){
	// allocate memory and then copy the input string
		src_ptr = new char[strlen(in_str)+1];
		strcpy(src_ptr, in_str);
		token_start_ptr = NULL;
		c_token = src_ptr;
	}
	inline char * current()
	{
		if (c_token == src_ptr)
			next();
		return token_start_ptr;
	}
	static const int print_width = 20;
	inline void print()
	{
		cout << setw(print_width) << left << current();
	}
	~tokenizer()
	{
		delete[] src_ptr;
	}
	char * next()
	{
		while ( * c_token && !isalpha(* c_token) ) // skip non-aplpa chars
			c_token ++;
		token_start_ptr = c_token;
		while ( * c_token && isalpha(* c_token) ) // skip aplpa chars
			c_token ++;
		if( !* c_token ) // end of string? true if *c_token == NULL
			// here token_start_ptr is either NULL or on the last token!
			return token_start_ptr;  
		* c_token ++ = NULL; //  c_token is on non-aplha;  
							 // make the token NULL terminated and then skip it
		return token_start_ptr;
	}
};


int main()
{
               const char * input = "Classes have the property of information hiding. This means "
		"that although class objects may know how to communicate with one another "
		"across well-defined interfaces, classes normally are not allowed to know "
		"how other classes is implemented-implementation details are hidden within "
		"the classes themselves. Surely it is possible to drive a car effectively "
		"without knowing the details of how engines, transmissions and exhaust "
		"systems work internally. We will see why information hiding is so crucial "
		"to good software engineering.";

	char * token_ptr;
	tokenizer t(input);

	while ( * t.next() != NULL )
	{
		t.print();
		if ( *t.next() != NULL ) t.print();
		if ( *t.next() != NULL ) t.print();
		if ( *t.next() != NULL ) t.print();
		cout << endl;
	}

	return 0;
}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

If you are using c++ why use character pointers? Just include <string> and use the std::string class becuse its much easier to use.

All you need to do is define a class or structure with two objects: std::string word and int count.. Then you could create a <vector> of that class/structure

As for the code you posted -- its too unformatted for me to bother reading. The next time you post put the code in [code] [/code] tags.

Edited 6 Years Ago by Ancient Dragon: n/a

Thank you for your help! I will remember that when I put code it. Like I said, I am new to all of this.

Just reposting my code correctly.

// This program counts word occurence in a given text

#include <iostream>
#include <iomanip>

using namespace std;

class tokenizer
{
char * src_ptr;
char * c_token;
char * token_start_ptr;
public: 
tokenizer(const char * in_str){
// allocate memory and then copy the input string
src_ptr = new char[strlen(in_str)+1];
strcpy(src_ptr, in_str);
token_start_ptr = NULL;
c_token = src_ptr;
}
inline char * current()
{
if (c_token == src_ptr)
next();
return token_start_ptr;
}
static const int print_width = 20;
inline void print()
{
cout << setw(print_width) << left << current();
}
~tokenizer()
{
delete[] src_ptr;
}
char * next()
{
while ( * c_token && !isalpha(* c_token) ) // skip non-aplpa chars
c_token ++;
token_start_ptr = c_token;
while ( * c_token && isalpha(* c_token) ) // skip aplpa chars
c_token ++;
if( !* c_token ) // end of string? true if *c_token == NULL
// here token_start_ptr is either NULL or on the last token!
return token_start_ptr; 
* c_token ++ = NULL; // c_token is on non-aplha; 
// make the token NULL terminated and then skip it
return token_start_ptr;
}
};


int main()
{
const char * input = "Classes have the property of information hiding. This means "
"that although class objects may know how to communicate with one another "
"across well-defined interfaces, classes normally are not allowed to know "
"how other classes is implemented-implementation details are hidden within "
"the classes themselves. Surely it is possible to drive a car effectively "
"without knowing the details of how engines, transmissions and exhaust "
"systems work internally. We will see why information hiding is so crucial "
"to good software engineering.";

char * token_ptr;
tokenizer t(input);

while ( * t.next() != NULL )
{
t.print();
if ( *t.next() != NULL ) t.print();
if ( *t.next() != NULL ) t.print();
if ( *t.next() != NULL ) t.print();
cout << endl;
}

return 0;
}
This article has been dead for over six months. Start a new discussion instead.