// SP1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
#define BLOCKSIZE 262144
#define WORDSIZE 32
#define WORDSIZEPLUS1 33
struct st {
char word[WORDSIZEPLUS1];
};
vector<struct st> vec[WORDSIZE];
bool is_letter(char c) {
if ((c>='a' && c<='z')||(c>='A' && c<='Z')||(c>='0' && c<='9')) return true;
else return false;
}
void add_word_to_list(char cur[WORDSIZEPLUS1]) {
int vec_num=strlen(cur)-1;
struct st curst;
bool fresh = true;
for(int i=0; i<vec[vec_num].size(); i++)
{
if (!strcmp(vec[vec_num].at(i).word,cur))
{
fresh = false;
break;
}
}
if (fresh)
{
strcpy(curst.word, cur);
vec[vec_num].push_back(curst);
}
}
void make_word_list(FILE * work_file) {
bool word_exist=false;
char line[BLOCKSIZE];
char current_word[WORDSIZEPLUS1];
int i=0,j=0,len;
while (fgets(line, BLOCKSIZE, work_file)) {
//printf(".");
if (!word_exist) for(int k=0; k<WORDSIZEPLUS1; k++) current_word[k]='\0';
len=strlen(line);
for(i=0; i < len; i++) {
if (is_letter(line[i])) {
if (j<WORDSIZE) {
current_word[j]=line[i];
j++;
word_exist = true;
}
}
else if (word_exist) {
add_word_to_list(current_word);
j=0;
word_exist = false;
for(int k=0; k<WORDSIZEPLUS1; k++) current_word[k]='\0';
}
}
}
if (word_exist) add_word_to_list(current_word);
}
void print_list() {
for(int i = 0; i<WORDSIZE; i++) {
if (!vec[i].empty()) printf("\n%d: ", i+1);
while (!vec[i].empty())
{
printf("%s ", vec[i].back().word);
vec[i].pop_back();
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
FILE * work_file;
char file_name[64];
printf("\nEnter the path to the text file: ");
scanf("%s", file_name);
work_file=fopen(file_name, "r");
make_word_list(work_file);
print_list();
getchar();
getchar();
return 0;
}