In: Computer Science
Program Requirements
Program Output
Your output should look like this:
Line 8: using(0) namespace(6) <== using
occurs at position 0 on line 8, namespace occurs at position 6 on
line 8 Line 10: const(0) int(6) Line 12: void(0) const(19) Line 13: void(0) char(20) int(32) const(48) Line 14: bool(0) const(24) char(30) const(42) Line 15: void(0) char(17) Line 16: void(0) Line 17: void(0) Line 19: int(0) Line 21: const(4) ... Number of keywords found = ?? <== Add this line at the end of your output, replace ?? with the correct number |
Program Hints
The keyword file looks like this:
for if nullptr break int long sizeof return short else friend const static_cast ... |
The C++ program file looks like this:
#include <cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <fstream> #include <iostream> #include <string> using namespace std; const int DictionarySize = 23907; void getDictionary(const string& filename,string*); void spellCheckLine(char* line, int lineNumber, const string* dictionary); bool wordIsInDictionary(const char* word, const string* dictionary); void toLowerCase(char* text); ... |
istream::getline example
ifstream fin("oldass3.cpp"); ... char buffer[132]; ... fin.getline(buffer,sizeof(buffer)); // store a line from the input file into buffer |
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string testline;
string codeline;
vector<string> word;
vector<string> app;
//opening keywords text file
ifstream Test("keywords.txt");
if (!Test)
{
cout << "There was a problem opening the file. Press any key to close.\n";
return 0;
}
while (Test)
{
Test >> testline;
//cout << testline << endl;
word.push_back(testline);
}
//Dynamically allocating keyords to array
for (int i = 0;i < word.size() - 1;i++)
//cout << word[i] << "(" << i << ")" << endl;
//sorting the keywords
sort(word.begin(), word.end());
//for (string &s : word)
//cout << s << "\n ";
cout << "output" << endl;
//opening the input file
ifstream code("input.txt");
if (!code)
{
cout << "There was a problem opening the file. Press any key to close.\n";
return 0;
}
int n = 0;
int count = 1;
int map = 0;
int g = 0;
bool c = true;
while (code)
{
code >> codeline;
for (int j = 0;j < word.size();j++)
{
if (code.peek() == '\n')
{
count = count + 1;
n = 0;
if (word[j] == codeline)
{
c = true;
cout << "line[" << count << "]";
cout << codeline << "[" << n << "]";
cout << endl;
// n = n+sizeof (codeline);
n = n + codeline.length();
map = map + 1;
break;
}
else
c = false;
break;
}
else
{
if (word[j] == codeline)
{
c = true;
cout << "line[" << count << "]";
cout << codeline << "[" << n << "]";
cout << endl;
n = n + codeline.length();
map = map + 1;
break;
}
else
c = false;
}
}
if (c == false && n != 0)
n = n + codeline.length();
}
cout << "Total no of keywords found" << map << endl;
system("pause");
return 0;
}
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================