counting words



I have tried the code below

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
#include <iostream>
#include <string>
using namespace std;

// function prototype
int countWords(char*);
int countWords(string);


int main()
{
	char* newPtr = nullptr;

	const int SIZE = 101;
	char usrInput[SIZE];

	newPtr = usrInput;


    cout << " Word Counter" << endl;
	fstream "Gettysburg.txt", ios::";out | ios::";binary);
	fstream "output.txt", ios::";out | ios::";binary);
	cin.getline(newPtr, SIZE);
	//getline(cin, sent);

	//display the number of words contained file and output them to a textfile
	cout << " Correct total of words counted in Gettysburg file are " << countWords(newPtr)  << endl;
	cout << "  check the output file " << countWords(newPtr) << " words" << endl;
	fstream "output.txt", ios::";out | ios::";binary);



	return 0;
}

//======================================================================
// function definition - counts how many words are in the string that is passed as an arg
int countWords(char* sentence)
{
	int totalWords = 0; // counts words

	if (*(sentence) != '\0') // if some input then there is at least 1 word
		++totalWords;

	for (int count = 0; *(sentence+count) != '\0'; count++)
	{
		if (*(sentence + count) == ' ')
		{
			++totalWords;
		}
	}
	return totalWords;
}


Last edited on
There are many ways to do this but here is one using the strtok() function and by a careful selection of characters in the the separators[] array as shown.

Note there are a few discrepancies due to the 100 character line limit and just cutting and pasting the source file. Each line in the original needs to be properly terminated or the 100 is substantially increased.

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
#include <cstdio>
#include <cstring>
#include <iostream>

const int MAX_LINE_LENGTH = 100;

int main(int argc, char* argv[])
{
	FILE* source;
	FILE* destination;

	char line[ MAX_LINE_LENGTH ];
	char separators[]   = "-?!. ,\t\n";
	char* token;

	const char* format = "%s ";

	source = fopen( "gettysburg.txt", "r" );
	destination = fopen( "gb_output.txt", "w" );
    
    int word_count{0};

	if(  source != NULL )
	{
		while( fgets( line, MAX_LINE_LENGTH, source ) != NULL )
		{
			token = strtok( line, separators );
			while( token != NULL )
			{
                word_count++;
                printf( format, token );
				fprintf( destination, format, token );
				token = strtok( NULL, separators );
			}
		}
		printf("\n +++ ENDS +++\n");
	}
	else
		printf( "fgets error\n" );
    
    std::cout << "No. of words: " << word_count << '\n';

	fclose( source );
	fclose( destination );

	return 0;
}


Four score and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal Now we are engaged in a great civil war testing whether that nation or any nation so conceived and dedicated can long endure We are met on a great battlefield of that war We have come to dedicate a portion of that fie ld as a final resting place for those who here gave their lives that that nation might live It is altogether fitting and proper that we should do this But in a larger sense we can not dedicate we can not consecrate we can not hallow this ground The brave men living and dead who stru ggled here have consecrated it far above our poor power to add or detract The world will little n ote nor long remember what we say here but it can never forget what they did here It is for us th e living rather to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced It is rather for us to be here dedicated to the great task remaining before u s that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion that we here highly resolve that these dead shall not have died in vain that this nation under God shall have a new birth of freedom and that government of t he people by the people for the people shall not perish from the earth 
 +++ ENDS +++
No. of words: 276
Program ended with exit code: 0
it shows 278 words in my end with your code
it needs to be 276 ; you also have printf which I can't use

Thank you for the hard effort !

I don't need to set a max to 100, you can delete that , I just should you an example!
Last edited on
As the first post is C++, this can be easily done in C++. If you extract a string from a stream, it extracts up-to a white space char. Assuming words are separated by white-space, then just counting the number of words extracted will give the number of words. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::ifstream ifs("gettysburg.txt");

	if (!ifs.is_open())
		return (std::cout << "File cannot be opened\n"), 1;

	std::ofstream ofs("output.txt");
	int cnt {};

	for (std::string word; ifs >> word; ++cnt);

	std::cout << cnt << " words\n";
	ofs << "Correct total of words counted in Gettysburg file are " << cnt << '\n';
}


Giving:


276 words

c:\MyProgs>type output.txt
Correct total of words counted in Gettysburg file are 276

Topic archived. No new replies allowed.