Instead of entering text to console, I want to use the text from a text file and use this function and method to do a concordance count of words and how many times they appear in the text file.
check line 137
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#ifndef _DEPRECATION_DISABLE /* One time only */
#define _DEPRECATION_DISABLE /* Disable deprecation true */
#if (_MSC_VER >= 1400) /* Check version */
#pragma warning(disable: 4996) /* Disable deprecation */
#endif /* #if defined(NMEA_WIN) && (_MSC_VER >= 1400) */
#endif
#define TEXTMAX 999999//maximum text length
#define WORDMAX 20//maximum word length
using namespace std;
unsigned int GetFileLength(std::string FileName)
{
std::ifstream InFile(FileName.c_str());
unsigned int FileLength = 0;
while (InFile.get() != EOF) FileLength++;
InFile.close();
return FileLength;
}
struct node
{
char word[WORDMAX];
int count;
struct node *next;
};typedef struct node nd;
void Insert(nd **head, char word[], int num)//insert
{
nd *p, *temp;
int cmp;
temp = (nd*)malloc(sizeof(nd));
p = *head;
strcpy(temp->word,word);
temp->count = num;
if(*head == NULL)
{
*head = temp;
temp->next = NULL;
}
else
{
cmp = strcmp(word,p->word);//compare alphabetically
if(cmp < 0)
{
*head = temp;//insert the word which becomes the new head
temp->next = p;
}
else
{
while(p->next!=NULL && (strcmp(word, p->next->word))>0)//look for appropriate place
{
p = p->next;
}
temp->next = p->next;
p->next = temp;
}
}
}
int Search(nd **head, char word[], nd **tempPointer)//searches for the word in the linked list
{
nd *p;
p = *head;
int count = 0, cmp;
while(p!=NULL)
{
cmp = strcmp(p->word, word);//compare the two strings
if(cmp == 0)//word found
{
count++;//count the occurence of that word
*tempPointer = p;//store the address
}
p = p->next;
}
return count;
}
void Display(nd **head)//display
{
nd *p;
p = *head;
printf("\nIndex:");
puts("\n------");
while(p!=NULL)
{
printf("\n%s = %d",p->word, p->count);
p=p->next;
}
printf(" \n");
}
const size_t SIZE = 1220;
void main()
{
int myOption;
char length;
nd *list = NULL, *tempPointer = NULL;
char text[TEXTMAX], word[WORDMAX];
int textLength=0, i, tempIndex = 0, count=0;
int c;
FILE *file;
file = fopen("text.txt", "r");
if (file)
{
while ((c = getc(file)) != EOF)
putchar(c);
//fclose(file);
}
//HOW DO I GET THE FILE ABOVE TO "text" variable?
gets(text);
textLength = strlen(text);// get length of text
if(textLength < TEXTMAX)//did not reach the maximum text limit, OK
{
fflush(stdin);
for(i=0;i<=textLength;i++)//traverse each character in the string
{
if(text[i] == ' ' || i == textLength)//if the char is a space and a char before it is not also a space then it marks as the end of a word
{
word[tempIndex] = '\0';//add an terminator to the char word being stored
count = Search(&list,word, &tempPointer);//search for the word in the existing list
if(count!=0)//word already exists in the list
{
fflush(stdin);
tempPointer->count += count;//update the count
}
else
{
if(word[tempIndex-1]!='\0' && word[0]!=' ' )//check if wat to be inserted is not a space
{
Insert(&list, word, 1);//insert
}
}
tempIndex = 0;//reset the index of temp string char
}
else if(text[i] != '.' && text[i] != ',' && text[i] != '?' && text[i] != '!' && text[i]!=' ')//char is a letter or a num but exclude some special chars
{
word[tempIndex] = tolower(text[i]);//covert to lower case and store the char to the string char array
tempIndex++;//increment the index of the char array
}
}
Display(&list);//display the index
}
else
{
puts("\nYou have reached beyond the maximum text limit. Please minimize your words and try again.");
}
getch();
}