Write a program that reads a C++ source-code file and reports the occurrence of each keyword in the file. Here is a sample run:

```

Enter a C++ source file name: Welcome.cpp
int occurs 3 times
void occurs 1 time
...
static occurs 1 time

```
Suppose a set of keywords is already given as follows:
```
set s;
s.insert("asm");
s.insert("auto");
s.insert("bool");
...
s.insert("while");
```
So you don’t need to populate the set in the program.

```
#include
#include
#include
#include
#include
using namespace std;

int main()
{
string filename;
cout << "Enter a file name: ";
cin >> filename;

ifstream input(filename.c_str());

set s;
s.insert("asm");
s.insert("auto");
s.insert("bool");
s.insert("break");
s.insert("case");
s.insert("catch");
s.insert("char");
s.insert("class");
s.insert("const");
s.insert("const_cast");
s.insert("continue");
s.insert("default");
s.insert("delete");
s.insert("do");
s.insert("dynamic_cast");
s.insert("else");
s.insert("enum");
s.insert("explicit");
s.insert("extern");
s.insert("false");
s.insert("float");
s.insert("for");
s.insert("friend");
s.insert("goto");
s.insert("if");
s.insert("inline");
s.insert("int");
s.insert("log");
s.insert("long");
s.insert("mutable");
s.insert("namespace");
s.insert("new");
s.insert("operator");
s.insert("private");
s.insert("protected");
s.insert("public");
s.insert("register");
s.insert("reinterpret_cast");
s.insert("return");
s.insert("short");
s.insert("signed");
s.insert("sizeof");
s.insert("static");
s.insert("static_cast");
s.insert("struct");
s.insert("switch");
s.insert("template");
s.insert("this");
s.insert("throw");
s.insert("true");
s.insert("try");
s.insert("typeof");
s.insert("typeid");
s.insert("typename");
s.insert("union");
s.insert("unsigned");
s.insert("using");
s.insert("virtual");
s.insert("void");
s.insert("volatile");
s.insert("wchar_t");
s.insert("while");

int count = 0;
map map1;
map::iterator p;
string word;

while (!input.eof())
{
input >> word;

set::iterator p1 = s.find(word);

if (p1 == s.end()) continue; // word is not in the set s

p = map1.find(word);
if (p == map1.end()) // word not in map1
{
map1.insert(map::value_type(word, 1));
}
else
{
int count = p->second;
count++;
map1.erase(word);
map1.insert(map::value_type(word, count));
}
}

for (p = map1.begin(); p != map1.end(); p++)
cout << "number of occurrences for " << p->first << " is " << p->second << endl;

return 0;
}
```

Computer Science & Information Technology

You might also like to view...

ScreenTips display small boxes with descriptive, helpful text when you point to a command or control

Indicate whether the statement is true or false

Computer Science & Information Technology

The transform that rotates a shape around a point, but otherwise leaves it unchanged, is known as a:

a) TranslateTransform b) SkewTransform c) ScaleTransform d) none of the above

Computer Science & Information Technology