......................

Word Counter

Posted by Unknown On Friday, September 3, 2010 0 comments

/***************************************
 APPLICATION : Word Counter
 CODED BY    : Ankit Pokhrel
 COMPILED ON : Borland C++ Ver 5.02
 DATE     : 2010 - August - 24
***************************************/

#include "fstream.h"
#include "conio.h"
#include "string.h"

struct TREE //structure to represent a tree
{
 char data[20];
 int count;
 struct TREE *right,*left,*root;
};

typedef TREE tree;

tree *New;
void create() //function to create a node
{
 New = new tree;
 New -> count = 1;
 New -> left = NULL;
 New -> right = NULL;
}

tree* search(tree *t,char *nme)
{
 tree *temp = t;
 if(strcmpi(temp -> data,nme) == 0) //if first node
  return temp; //return first node

 if(strcmpi(nme,temp -> data) < 0)
  temp = temp -> left; //go to left

 else
  temp = temp -> right; //go to right

 while(temp != NULL) //until end
  {
   if(strcmpi(nme,temp -> data) == 0) //if found
    return temp; //return node

   if(strcmpi(nme,temp -> data) < 0) //if data is less than current node
    temp = temp -> left; //go to left

   else
    temp = temp -> right; //go to right
  }

 return NULL; //data not found
}

void insert(tree *&t,char *nme)
{
 if(t == NULL) //if tree doesn't exists
  {
   t = new tree; //create a tree
   strcpy(t -> data,nme); //insert data
   t -> count = 1;
   t -> left = NULL;
   t -> right = NULL;
   t -> root = t; //initialize root
   return;
  }

 tree *temp = search(t,nme);
 if(temp != NULL)
  {
   temp -> count++; //Increase Count
   return;
  }

 //Insert as a new node
 tree *trav = t;
 if(strcmpi(nme,trav -> data) < 0)
  {
   if(trav -> left == NULL)
    {
     create();
     strcpy(New -> data,nme);
     trav -> left = New;
    }
   else
    insert(trav -> left,nme); //go to left
  }

 else
  {
   if(trav -> right == NULL)
    {
     create();
     strcpy(New -> data,nme);
     trav -> right = New;
    }
   else
    insert(trav -> right,nme); //go to right
  }
}

void inorder(tree *t) //Inorder Traversal
{
 if(t != NULL)
  {
   inorder(t -> left);
   cout << t -> data << " - " << t -> count << endl;
   inorder(t -> right);
  }
}

int indicator(char flenme[]) //Pastes "0z4978" at Last on a file to indicate the End of File
{
 ifstream infile(flenme);
 if(infile.fail()) //check if File exists or not
  return 0;
 infile.close();

 ofstream outfile(flenme,ios::app); //open File in Append mode
 outfile << "\n\n\n";
 outfile << "0z4978"; //paste "0z4978"
 outfile.close(); //close file
 return 1;
}

int main()
{
 char filename[15],word[50];
 tree *tr = NULL;
 cout << "Enter Filename : ";
 cin >> filename;
 int done = indicator(filename);
 if(!done)
  {
   cout << "\nFile Doesn't Exists";
   cout << "\nPlease try again...";
   getch();
   return 0;
  }

 ifstream infile(filename); //open file in Read-Only mode

 while(strcmp(word,"0z4978") != 0) //until EOF
 {
  infile >> word; //scan a word from file
  int len = strlen(word);

  if(word[0] == '.' || word[0] == '(' || word[0] == ',' || word[0] == '<'
   || word[0] == ':' || word[0] == '\'' || word[0] == '\"' || word[0] == '@'
   || word[0] == '$' || word[0] == '%')
    {
     for(int i = 0;i < len;i++)
      word[i] = word[i + 1]; //exclude Special Characters at Start of string
     len--;
    }

  if(word[len - 1] == '.' || word[len - 1] == ',' || word[len - 1] == '>'
   || word[len - 1] == ')' || word[len - 1] == ':' || word[len - 1] == '?'
   || word[len - 1] == '@' || word[len - 1] == '$' || word[len - 1] == '%'
   || word[len - 1] == '\"' || word[len - 1] == '\'')
   word[len - 1] = '\0'; //exclude Special Characters at End of string

  if(strcmp(word,"0z4978") != 0)
   insert(tr,word); //insert word in tree
 }

 clrscr();
 infile.close();
 cout << "Total Words in File " << filename << endl << endl;
 inorder(tr); //display Output
 cout << endl << "Press any key to halt....";
 getch();
 return 0;
}                                                


No comments:

Post a Comment

Leave Feedback about this BLOG