Work Count-Unix Utilities

Im running rand-test.sh for this code but all I keep getting is 16 tests failed. I want to know how I can improve this code to include the -w -c -L -l options.

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
109
110
111
  #include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <set>
using std::set;
#include <vector>
using std::vector;

unsigned long countLines(const string&s, set<string>& ul);
unsigned long countWords(const string& s, set<string>& wl);
unsigned long countCharacters(const string& s);
void makeCaps(string& s);

int main() {


	unsigned long lines = 0;
	unsigned long words = 0;
	unsigned long characters = 0;
	set<string> uniqueWords;
	set<string> uniqueLines;


	string line; // taking one line at a time
	getline(cin,line);

	lines = countLines(line,uniqueLines);
	words = countWords(line, uniqueWords);
	characters = countCharacters(line);

	while (getline(cin,line)) {
		lines += countLines(line,uniqueLines);
		words += countWords(line, uniqueWords);
		characters += countCharacters(line);
	}


	cout << lines << '\t';
	cout << words << '\t';
	cout << characters << '\t';
	cout << uniqueLines.size() << '\t';
	cout << uniqueWords.size() << endl;

	return 0;
}

unsigned long countCharacters(const string& s) {
	char tab = '\t'; 
	long chara = 0, tabs = 0;
	for (long i = 0; i < s.length(); i++) {
		if (s[i] == (tab)) {
			tabs++;
		} else {
			chara++;
		}
	}
	chara++; 
	return chara + tabs;
}

unsigned long countWords(const string& s, set<string>& wl) {
	vector<string> words; 
	int startOfWord, endOfWord; 

	for (int i = 0 ; i < s.length() ; i++) {
		if ((s[i] >= 65 && s[i] <= 90) || (s[i] >= 97 && s[i] <= 122)) {
			startOfWord = i;
			for (int j = i ; j < s.length() ; j++ ){
				if (!((s[j] >= 65 && s[j] <= 90) || (s[j] >= 97 && s[j] <= 122))){
					endOfWord = j;
					break;
				} else if (j == s.length()-1){
				
					endOfWord= j+1;
					break;
				}
			}
			i = endOfWord;
			string oneWord = s.substr(startOfWord, endOfWord-startOfWord);
			
			makeCaps(oneWord); 
			words.push_back(oneWord);
		}
	}
	for (int k = 0 ; k < words.size(); k++)
		wl.insert(words[k]);

	return words.size();
}

unsigned long countLines(const string& s, set<string>& ul){
	
		vector<string> count;
		count.push_back(s);

		
		ul.insert(s);

		return count.size();
}

void makeCaps(string& s) {
	for (int i = 0; i < s.length(); i++) {
		if (int(s[i]) >= 97 && int(s[i]) <= 122) {
			s[i] -= 32;
		}
	}
}
Command line options are passed to program as arguments for main().
The main has a second signature for that:
int main( int argc, char **argv )

You have to check what options the program was called with and choose actions accordingly.
Topic archived. No new replies allowed.