Expand|Select|Wrap|Line Numbers
- #include "IndexADT.h"
- int IndexInit(IndexADT* word)
- {
- word->head = NULL;
- word->wordCount = 0;
- return 1;
- }
- int IndexCreate(IndexADT* wordList,char* argv)
- {
- FILE* fp;
- char* tokens[500];
- char* tokens2;
- int i;
- if((fp = fopen(argv,"r")) == NULL)
- {
- fprintf(stderr, "File %s: open error\n", argv);
- exit(1);
- }
- else
- while(fgets(argv,500, fp) != NULL)
- {
- i = 0;
- tokens2 = strtok(argv," ");
- while(tokens2 != NULL)
- {
- tokens[i] = tokens2;
- tokens2 = strtok(NULL," ");
- addToADT(wordList,tokens[i]);
- i++;
- }
- }
- fclose(fp);
- return 1;
- }
- void addToADT(IndexADT* index,char* word)
- {
- WordNode *newWordNode;
- WordNode *current, *previous;
- PosNode *curPos;
- PosNode *prevPos;
- PosNode *newPositionNode;
- newWordNode = malloc(sizeof(WordNode));
- if(newWordNode == NULL)
- {
- printf("error, please reload the program\n");
- }
- if((newPositionNode = malloc(sizeof(PosNode))) == NULL)
- {
- printf("error, please reload the program\n");
- }
- newPositionNode->positionNum = 0;
- previous = NULL;
- strcpy(newWordNode->word, word);
- /*If first node is empty*/
- if((index -> head) == NULL)
- {
- newWordNode->next = index->head;
- index->head = newWordNode;
- index->wordCount++;
- newPositionNode->positionNum = index->wordCount;
- current = index->head;
- current->wordNum = index->wordCount;
- current->posHead = newPositionNode;
- printf("indexWordCount = %d\n", index->wordCount);
- printf("currentWordNum = %d\n", current->wordNum);
- printf("positionNodeNum = %d\n", newPositionNode->positionNum);
- return;
- }
- current = index -> head;
- while(current != NULL && (strcmp(current->word,newWordNode->word) < 0))
- {
- previous = current;
- current = current-> next;
- /*PROBLEM IS HERE*/
- if(strcmp(previous->word,newWordNode->word) == 0)
- {
- printf("POSITION HERE\n");
- curPos = current->posHead;
- while(curPos != NULL)
- {
- prevPos = curPos;
- curPos = curPos->next;
- }
- if(prevPos == NULL)
- {
- index->wordCount++;
- newPositionNode->positionNum = index->wordCount;
- newPositionNode->next = current->posHead;
- current->posHead = newPositionNode;
- return;
- }
- else
- {
- index->wordCount++;
- newPositionNode->positionNum = index->wordCount;
- prevPos -> next = newPositionNode;
- newPositionNode->next = curPos;
- return;
- }
- return;
- }
- }
- if(previous ==NULL)
- {
- newWordNode->next = index->head;
- index->head = newWordNode;
- }
- else
- {
- previous -> next = newWordNode;;
- newWordNode->next = current;
- index->wordCount++;
- newPositionNode->positionNum = index->wordCount;
- previous->posHead = newPositionNode;
- printf("positionNodeNum = %d\n", newPositionNode->positionNum);
- }
- current = index -> head;
- while(current != NULL)
- {
- printf("%s n Ascii %i\n",current->word,current->word);
- current = current-> next;
- }
- printf("%d\n",index->wordCount);
- }
- /*HEADER FILE IndexADT.h*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct positionNode
- {
- int positionNum;
- struct positionNode* next;
- }PosNode;
- typedef struct wordNode
- {
- char word[35];
- int wordNum;
- struct wordNode* next;
- PosNode* posHead;
- }WordNode;
- typedef struct wordList
- {
- WordNode* head;
- int wordCount;
- }IndexADT;
- int IndexInit(IndexADT*);
- int IndexCreate(IndexADT*, char*);
- int IndexDisplay(IndexADT*);
- int IndexSearch(IndexADT*, char*);
- int IndexRecompose(IndexADT*);
- int IndexClose(IndexADT*, char*);
- void addToADT(IndexADT*,char*);
- /* assign1.c */
- #include "IndexADT.h"
- int main(int argc,char** argv)
- {
- IndexADT worldList;
- IndexInit(&worldList);
- if(argc != 2)
- {
- fprintf(stderr, "Invalid usage: %s \n", argv[0]);
- exit(1);
- }
- IndexCreate(&worldList,argv[1]);
- return EXIT_SUCCESS;
- }
unix% gcc -ansi -wall -pedantic -o assign1 assign1.c IndexADT.c
To run
unix% assign1 textfile.txt
Text file
"There is a chicken. The chicken is very stupid, i'm going to eat him for lunch.
What else is there to eat for lunch".
I've got this problem where i read in the file using fgets, then i strtok the words out one by one and then add each word to my linked list. The idea of this work i'm suppose to do is, to read in the file, and display it back on the screen with the position number of the word as the result .I would to token out the words but any words that occur more than twice will print out the word once but displays two or more place of the position of the word
example:
"There is a chicken. It is stupid"
word position number
There 0
is 1,5
a 3
etc...
my problem is tat i tokenize the word, and put it in my linked list.When i traverse the list, any words that occur twice like the example "is" will not enter my strcmp(...,...) == 0. it should be possible to enter my strcmp bcos of the comparison to be equal to 0 but one problem i figure out is that when i convert the word/token in ascii code(for debugging), they are not the same and discovered that there is a special character behind the word(when convert to char) that i can't get rid of because it's non existent to the keyboard.
Can someone help show me another way for me to tokenize the word without getting the special chars at the back of my tokens(clearn-free) and allow me to use strcmp.Or at least show me how to get rid of the special chars at the back of the token/word.? Please try out my code and take a look. thank you
Let me love C programming by figuring this out!
NoobCProgrammer