plz help me Word counting program...

I desperately need a program like

1.in cmd, type programname.exe myword input.txt
2.then the program will read input.txt and count words
example of input.txt)
%#^dog&^(cat!#$human

it will count 3 words, dog,cat,human. ignore which is not alphanumeric.

3.and the program will compare those words with myword. when done, output is

numberOfMyWord / entireWord
(in console)


plz anybody help me sorry T-T
Have you made any attempt at writing this yourself? I'm guessing, but is this a homework/assignment you've been given to do?
just count all the spaces and add +1 at the end, since there shouldn't be a space after the last word
just count all the spaces and add +1 at the end, since there shouldn't be a space after the last word


@Darkmaster - didn't you see his example text, there are no spaces. But this program should be quite simple to write. Shall we see if he has made any attempt at it before we assist?
I made a function which maybe remove non-alphanumeric...

void RemoveSpecial(string &in)
{
int len = in.size();
int i;

for(i=0;i<len;i++)
{
if( in[i] >= 'A' && in[i] <= 'Z' )
in[i] += ( 'a' - 'A' );
else if( in[i] >= 'a' && in[i] <= 'z' );
else in[i] = ' ';
}
}


but...that's all. I even don't know how can I open .txt
I don't think this work, which I learn from another teacher.
int main(int argc,char *argv[]) //main function argument
{

FILE *file_input;
file_input=fopen(argv[2],"r")
}
hm i totally missed that, sry.

http://www.cplusplus.com/doc/tutorial/files/
Last edited on
#include <string>
#include<iostream>
#include<cctype>
#include<cstring>
#include<fstream>

using namespace std;

int main( int argc, char* argv[] )
{
// argv[0] - programname.exe
// argv[1] - your word
// argv[2] - input.txt
// argc - number of parameters in cmd

if( argc != 3)
{
cout << "Missing parameters\n";
cin.get();
return 0;
}


string word = argv[ 1 ]; // Your word

ifstream in( argv[2] );
string str;

// Reading from file into str
while( !in.eof() )
{
string temp;
getline( in, temp );
str.append( temp );
str+='\n';
if( in.eof() )
break;
}

int count = 0;



string :: iterator it = str.begin();

// Counting number of words
do
{
if( isalpha( *it ) )
{
++count;
while( ++it != str.end() && isalpha(* it ) );
}
if( it == str.end() )
break;

++it;
}
while( it != str.end() );

cout << "\nNumber of words = " << count << endl;

in.close();
cin.get();
return 0;
}

I haven't done step 3.
Try it yourself .
Last edited on
I almost got it but have some error...
when there are same word, the program count it as one word.
ex)
1.txt)
$^&%dog$%&cat^@human%&obiwan*^grrrrr((*obiwan
myprogram.exe obiwan 1.txt

1/6


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>


using namespace std;


void RemoveSpecial(string &in)
{
int len = in.size();
int i;

for(i=0;i<len;i++)
{

if( in[i] >= 'A' && in[i] <= 'Z' )
in[i] += ( 'a' - 'A' );
else if( in[i] >= '0' && in[i] <= '9' || in[i] >= 'a' && in[i] <= 'z');
else in[i] = ' ';
}
}



struct Word
{
string word;
int count;
};







int main(int argc,char *argv[]) //main function argument
{
string line,input;

vector<struct Word > wordList;
vector<struct Word >::iterator list;
map<string,int> M;
map<string,int>::iterator j;
struct Word data;




ifstream myfile (argv[2]);
if (myfile.is_open()) {
while ( myfile.good() ) {
getline (myfile,line);
RemoveSpecial(line);
stringstream ss(line);


while (ss >> input)
{
M[input]++;
}
}
for(j=M.begin(); j != M.end(); ++j)
{


data.word = j->first;
data.count = j->second;
wordList.push_back(data);
}

int scount = 0;
string y;
for(int i=0;i<wordList.size();i++)
{
y = wordList[i].word;
if( argv[1] == y) {
scount++;
}
}


cout << endl << scount << " / " << wordList.size();








myfile.close();
}



}

#include <string>
#include<iostream>
#include<cctype>
#include<cstring>
#include<fstream>

using namespace std;

int main( int argc, char* argv[] )
{
// argv[0] - programname.exe
// argv[1] - your word
// argv[2] - input.txt
// argc - number of parameters in cmd

if( argc != 3)
{
cout << "Missing parameters\n";
cin.get();
return 0;
}


string word = argv[ 1 ]; // Your word

ifstream in( argv[2] );
string str;

// Reading from file into str
while( !in.eof() )
{
string temp;
getline( in, temp );
str.append( temp );
str+='\n';
if( in.eof() )
break;
}

int count = 0;



string :: iterator it = str.begin();

// Counting number of words
do
{
if( isalnum( *it ) )
{
++count;
while( ++it != str.end() && isalnum(* it ) );
}
if( it == str.end() )
break;

++it;
}
while( it != str.end() );

cout << "\nNumber of words = " << count << endl;

in.close();
cin.get();
return 0;
}

I haven't done step 3.
Try it yourself .
Last edited on
thanks TTT, although there are some difference between my code...XD
I almost got it but have some error...
when there are same word, the program count it as one word.



This happens because in map key values ( in your code string is key value and int is mapped value ) are unique, so you cannot store the same word in map more then one time.
You can try to use multimap instead of map where key values are not unique.
Last edited on
How about the following code?

Use: program_name.exe cat c:\input.txt

where the word 'cat' is the word you are searching for and the 2nd argument is the path to the input.txt file to be read:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char* get_word(char** data)
{
	char* word = new char[strlen(*data)+1];
	memset(word, 0, strlen(*data));
	char* p = word;

	while(*data)
	{
		if (isalpha(**data))
			*(p++) = **data;
		else
			if (word[0])
				break;
		++*data;
	}

	return word;
}

void get_wordcount_and_matchcount(const char* data, const char* match, int* wordcount, int* matchcount)
{
	char* p = (char*)data;

	while(p && *p)
	{
		char* word = get_word(&p);

		if (word)
		{
			++(*wordcount);
			if (strcmp(match, word) == 0)
				++(*matchcount);
			delete[] word;
		}
	}
}

int main(int argc, char* argv[])
{
	if (argc != 3)
		return 1;

	char* myword = argv[1];
	FILE* file_input = fopen(argv[2], "r");

	if (file_input)
	{
		fseek(file_input, 0, SEEK_END);
		size_t size = ftell(file_input);
		char* data = new char[size+1];
		rewind(file_input);
		fread(data, sizeof(char), size, file_input);
		fclose(file_input);
		data[size] = '\0';

		int wordcount(0), matchcount(0);

		get_wordcount_and_matchcount(data, myword, &wordcount, &matchcount);

		printf("The number of words in input was: %d and the number of words that matched %s was: %d\n\n", wordcount, myword, matchcount);

		delete[] data;
	}

	return 0;
}
Last edited on

But it will be more convenient to pass wordcount and matchcount by reference, ie prototype will be:

void get_wordcount_and_matchcount(const char* data, const char* match, int & wordcount, int & matchcount);

and in function body instead of writing
++(*wordcount);
you'll write
++wordcount;
Last edited on
thanks but don't properly work.

1. input.txt instead of c:\input.txt -> crash( i use input.txt, not c:\input.txt because they are in same directory)

2.
input.txt
%^&asibasd4$%
asd^#(@dog
^&*f$obiwan12^&^obiwan12

when search obiwan,
2/7

when search obiwan12,
0/8

o_0?!
looks better than your previous one.


What previous attempt?
Sorry, mixed with hsw's code

thanks but don't properly work.


And I think it is because he used isalpha() instead of isalnum()

Replace isalpha() with isalnum() and then try again
Last edited on
thanks it perfectly working!!!

really thank you all XD
thanks it perfectly working!!!

really thank you all XD

@hsw2201 - do you understand what it is doing though?
yes I read code and searching internet for what i didn't know(like eof) and etc..
there are somethings i don't really understand yet but I hope I can discover them soon^^
Topic archived. No new replies allowed.