Problem counting number of words with less than 3 characters

I'm new to using this website/forum, so I'll say sorry ahead of time in case my post isn't quite the format it should be.

I'm trying to write a program that reads a text file, and keeps count of how many words in the text file have 3 letters or less.

First off, I'm kind of unsure whether or not to store the text file being read as a char or string, since I'm assuming I would need to read 1 character at a time(to check for spaces)

Secondly I'm really not entirely sure how I would check to see if a word had 3 letters or less, I've tried a few ways and all I've managed to do so far is count the number of total characters in the file, instead of how many characters each word contains.

This is what I have so far(sorry it's pretty incomplete, but there really isn't much to do since I can't figure it out)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	int counter = 0, letterCount = 0, wordCount = 0, i = 0;
	char nextChar;
	string fileName, getText, wordString;
	ifstream infile;
	InitializeFile(fileName, infile); //func:gets file name and opens it
	FailedFileErrorTrap(fileName, infile); //func: makes sure file exists
	infile >> nextChar;
	while(!infile.eof())
	{
		for(i = 0; i < nextChar; i++)
		if(!isspace(nextChar))
			wordString += getText;
		if(wordString.length() <= 5)
			counter++;
		infile >> nextChar;
	}
	cout << counter << endl;
	cout << nextChar << endl;


The output I get is
35
y


35 is the number of total characters in the text file, and 'y' is the last character in the text file.

Any help or tips I could get to make this work would be appreciated.
hmm. had to edit that I didn't read your code in full. (suggested use of .length)

7. infile >> nextChar;

Is this supposed to get the first character in the file? I'm not quite sure that the declaration i < z or i < a will work consistently. really would need a counter.
But I could be wrong.

I think instead of using char you should look extensively at the string class, type string into search, and click the first link.
http://www.cplusplus.com/reference/string/string/

namely things that will help you are, find_first_of, find, substr, end_pos etc.
Last edited on
Yeah, it was to get the first letter.

Inside the while loop
8. while(!infile.eof())
it's done again at the bottom of the loop, so it gets each character while it's not the end of the file.

I changed it to use a string instead and it looks like this now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	int counter = 0, letterCount = 0, wordCount = 0, i = 0;
	string fileName, getText, wordString;
	ifstream infile;
	InitializeFile(fileName, infile);
	FailedFileErrorTrap(fileName, infile);
	while(!infile.eof())
	{
		getline(infile, getText);
		for(i = 0; i < getText.length(); i++)
		{
		if(!isspace(getText[i]))
			wordString = getText;
		if(wordString.length() <= 5)
			counter++;
		}
	}
	cout << counter << endl;
	cout << wordString << endl;


and the output is
0
The little then theres another little one


Thats just some random words I threw into a text file to use for this, it's not supposed to make any sense.


I know I more or less need to read the string up until I hit a " " space character, and count how many letters there were before the space, then count the number of characters after the space until I hit another space, etc. until the end of the file.

I'm just not really sure what kind of logic I'd need to use to get it to work.
if(!isspace(getText[i]))
wordString = getText;
you the last line into wordstring. which im guessing is not what you want

someone more experienced will need to correct me because I think i might be wrong, but the logic would be: I'm am actually unsure of what your trying to accomplish with your program but so far I think just getting count of words less than 3 letters long? or deleting them?
1, open file
2, read first line in file, store in string variable
3, read first string up to a whitespace and store in a separate variable. (word variable.)
4, check the length of this string, process as required. (need to count words?)
5, step over the space into next pos and make sure this is not a space also.
6, recursive.
7, when at end of line grab next line and process as steps 3-6 above. until EOF.
8, display appropriate data.

Ill edit this post once I find some relative examples.
Last edited on

This first sample is a very simple use of length and concatenation.
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
#include <iostream>
using std::cout;

#include <string>
using std::string;

//---------------------------------------------

int main()
{
   //call contructor to instantiate s1
   string s1("this is one long string");

   cout << "s1 contains [" << s1 << "]\n";
   cout << "size = " << s1.size() << "\n";
   cout << "length = " << s1.length() << "\n";

   if (!s1.empty() )
      cout << "s1 is not empty\n";

   s1.clear(); //s1 now empty

   if (s1.empty())
      cout << "s1 is empty after clearing it\n";

   cout << "length = " << s1.length() << " after clearing\n";
   
   //instantiate s2 ... so constructor works with nothing given
   string s2;

   if (s2.empty())
      cout << "s2 is empty\n";

   // concatenating strings.
   s2="second string concatenated to first";
   string s3;

   s3= s1 + " " +  s2;
   cout <<  "String 3 = [ " << s3 << " ]" << endl;

   return 0;
}



This next sample shows uses of what you can look for and do with strings.
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

//---------------------------------------------

int main()
{
   string s1, s2, s3;

   //-------------------------------------------

   cout << "Enter a sentence containing spaces, then press enter\n";
   getline(cin, s1);
   cout << "You entered [" << s1 << "]\n";
   cout << endl;

   //-------------------------------------------

   cout << "Enter a sentence. Use the # char twice in the sentence, "
        << "then press enter.\n\n";
   cout << "For example look at what follows and type it in exactly\n\n";
   cout << "here is part#this is the second part # this is the end\n\n";

   getline(cin, s1, '#');
   getline(cin, s2, '#');
   getline(cin, s3);
   
   cout << endl;

   cout << "First  part is [" << s1 << "]\n\n";
   cout << "Second part is [" << s2 << "]\n\n";
   cout << "Third  part is [" << s3 << "]\n\n";
   cout << endl;

   //-------------------------------------------

   cout << "Enter a simple sentence and then press enter\n";
   getline(cin, s1);

   cout << "\nEnter a string to find in the sentence, it must exist: ";
   getline(cin, s2);

   size_t whereFound;
   
   whereFound = s1.find(s2); // s2 does exist in s1
   if (whereFound != string::npos)
      cout << "That string starts at position " << whereFound
           << " in the sentence.\n\n";

   cout << "Enter a string to find in the sentence, it must NOT exist: ";
   getline(cin, s2);
   
   whereFound = s1.find(s2); //s2 does not exist in s1
   if (whereFound == string::npos)
      cout << "That string does not exist in the sentence\n\n";

   cout << "Value of whereFound = " << whereFound << "\n";
   cout << "value of string::npos = " << string::npos << endl;

   cout << endl;

   //-------------------------------------------

   s1 = "Here is a new sentence";
   s2 = "is";
   whereFound = s1.find(s2); //search s1 for the string in s2

   if (whereFound != string::npos)
      cout << "Found [" << s2 << "] at position " 
           << whereFound << " in [" << s1 << "]\n\n";

   whereFound = s1.find(s2, whereFound+1);
   if (whereFound == string::npos)
      cout << "[" << s2 << "] does not exist after that position\n\n"; 

   
   s2="enterance";
   
   
   whereFound = s1.find(s2.c_str(),0,2); // looking for "en" in s1
   cout << "Found \"en\" at position " << whereFound 
        << " in [" << s1 << "]\n";

   whereFound = whereFound + 1; 
   whereFound = s1.find(s2.c_str(),whereFound,2); // looking for next "en" in s1
   cout << "Found NEXT \"en\" at position " << whereFound 
        << " in [" << s1 << "]\n";

   cout << endl;
   //-------------------------------------------
   char lookForCh = 'e';

   whereFound = s1.find(lookForCh,0); //first 'e'
   whereFound = whereFound+1;
   whereFound = s1.find(lookForCh,whereFound); //second 'e'
   whereFound = whereFound+1;
   whereFound = s1.find(lookForCh,whereFound); //third 'e'
   cout << "3rd '" << lookForCh << "' found at position "
        << whereFound << " in [" << s1 << "]\n";
   cout << endl;

   return 0;
}



see line 29-31 as an example you could do something like:
1
2
3
4
5
6
7
8
while (! inFile.eof())
{
   getline(inFile, word, ' \t\n');
   if (word.length<=3)
      small_word_count++;
   else 
      long_word_count++;
}

But that is basic, and off the top of my head so you would have to check getline API and make sure that is possible.
Last edited on
as another example, if you include a few more searches for 'e':
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 <iostream>
#include <string>
using namespace std;

int main()
{
   string s1="Here is a new sentence";
   char lookForCh = 'e';

   cout << "String 1= " << s1 << endl;
   whereFound = s1.find(lookForCh,0); //first 'e'
   cout << "1st '" << lookForCh << "' found at position "
        << whereFound << " in [" << s1 << "]\n";
   cout << endl;
   whereFound = whereFound+1;
   whereFound = s1.find(lookForCh,whereFound); //second 'e'
   cout << "2nd '" << lookForCh << "' found at position "
        << whereFound << " in [" << s1 << "]\n";
   cout << endl;
   whereFound = whereFound+1;
   whereFound = s1.find(lookForCh,whereFound); //third 'e'
   cout << "3rd '" << lookForCh << "' found at position "
        << whereFound << " in [" << s1 << "]\n";
   cout << endl;
   whereFound = whereFound+1;
   whereFound = s1.find(lookForCh,whereFound); //third 'e'
   cout << "4th '" << lookForCh << "' found at position "
        << whereFound << " in [" << s1 << "]\n";
   cout << endl;
   whereFound = whereFound+1;
   whereFound = s1.find(lookForCh,whereFound); //third 'e'
   cout << "5th '" << lookForCh << "' found at position "
        << whereFound << " in [" << s1 << "]\n";
   cout << endl;
   whereFound = whereFound+1;
   whereFound = s1.find(lookForCh,whereFound); //third 'e'
   cout << "6th '" << lookForCh << "' found at position "
        << whereFound << " in [" << s1 << "]\n";
   cout << endl;
   whereFound = whereFound+1;
   whereFound = s1.find(lookForCh,whereFound); //third 'e'
   cout << "7th '" << lookForCh << "' found at position "
        << whereFound << " in [" << s1 << "]\n";
   cout << endl;

   return 0;
}


the 7th 'e' does not exist so the value of wherefound is: 4294967295
Last edited on
last example that might help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// string::push_back
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
  string str;
  ifstream file ("test.txt",ios::in);
  while (!file.eof())
  {
    str.push_back(file.get());
  }
  cout << str;
  return 0;
}

This example reads an entire file character by character, appending each character to a string object using push_back.


and lastly an extended example for find:
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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

//---------------------------------------------

int main()
{
   string s1, s2;
   size_t whereFound, lastFound;

   //-------------------------------------------
  
     cout << "PRINT OUT STRINGS THAT ARE SEPARATED BY SPACES\n\n";

   s1 = "Returns a string object with its contents initialize to";

   whereFound = s1.find(" "); //find first space
   s2 = s1.substr(0, whereFound); //grab first group of chars up to first space
   cout << "s1 [" << s1 << "]\n";
   cout << "s2 [" << s2 << "]\n";

   lastFound = whereFound+1; //step over space
   whereFound = s1.find(" ", lastFound);
   s2 = s1.substr(lastFound, whereFound - lastFound); //grab next group 
   cout << "s2 [" << s2 << "]\n";

   lastFound=whereFound+1; //step over space
   whereFound = s1.find(" ", lastFound);
   s2 = s1.substr(lastFound, whereFound - lastFound); //grab next group 
   cout << "s2 [" << s2 << "]\n";

   lastFound=whereFound+1; //step over space
   whereFound = s1.find(" ", lastFound);
   s2 = s1.substr(lastFound, whereFound - lastFound); //grab next group 
   cout << "s2 [" << s2 << "]\n";
   cout << endl;
 
   return 0;
}


it is also possible to check end of line using npos I believe...
Last edited on
Topic archived. No new replies allowed.