467,880 Members | 1,265 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,880 developers. It's quick & easy.

Full code here,experts pls, how to tokenize so strcmp is possible???

Expand|Select|Wrap|Line Numbers
  1. #include "IndexADT.h"
  2.  
  3. int IndexInit(IndexADT* word)
  4. {
  5.    word->head = NULL;
  6.    word->wordCount = 0;
  7.    return 1;
  8. }
  9.  
  10. int IndexCreate(IndexADT* wordList,char* argv)
  11. {
  12.    FILE* fp;
  13.    char* tokens[500];
  14.    char* tokens2;
  15.    int i;
  16.  
  17.  
  18.    if((fp = fopen(argv,"r")) == NULL)
  19.    {
  20.       fprintf(stderr, "File %s: open error\n", argv);
  21.       exit(1);
  22.    }
  23.    else
  24.       while(fgets(argv,500, fp) != NULL)
  25.       {
  26.          i = 0;
  27.          tokens2 = strtok(argv," ");
  28.          while(tokens2 != NULL)
  29.          {
  30.             tokens[i] = tokens2;
  31.             tokens2 = strtok(NULL," ");
  32.             addToADT(wordList,tokens[i]);
  33.             i++;
  34.          }
  35.  
  36.  
  37.       }
  38.    fclose(fp);
  39.    return 1;
  40. }
  41.  
  42.  
  43.  
  44. void addToADT(IndexADT* index,char* word)
  45. {
  46.    WordNode *newWordNode;
  47.    WordNode *current, *previous;
  48.  
  49.    PosNode *curPos;
  50.    PosNode *prevPos;
  51.    PosNode *newPositionNode;
  52.  
  53.  
  54.    newWordNode = malloc(sizeof(WordNode));
  55.    if(newWordNode == NULL)
  56.    {
  57.       printf("error, please reload the program\n");
  58.    }
  59.  
  60.    if((newPositionNode = malloc(sizeof(PosNode))) == NULL)
  61.    {
  62.       printf("error, please reload the program\n");
  63.    }
  64.    newPositionNode->positionNum = 0;
  65.  
  66.  
  67.    previous = NULL;
  68.  
  69.    strcpy(newWordNode->word, word);
  70.  
  71.    /*If first node is empty*/
  72.    if((index -> head) == NULL)
  73.    {
  74.       newWordNode->next = index->head;
  75.       index->head = newWordNode;
  76.       index->wordCount++;
  77.       newPositionNode->positionNum = index->wordCount;
  78.       current = index->head;
  79.       current->wordNum = index->wordCount;
  80.       current->posHead = newPositionNode;
  81.       printf("indexWordCount = %d\n", index->wordCount);
  82.       printf("currentWordNum = %d\n", current->wordNum);
  83.       printf("positionNodeNum = %d\n", newPositionNode->positionNum);
  84.  
  85.       return;
  86.    }
  87.    current = index -> head;
  88.  
  89.  
  90.    while(current != NULL && (strcmp(current->word,newWordNode->word) < 0))
  91.    {
  92.       previous = current;
  93.       current = current-> next;
  94.            /*PROBLEM IS HERE*/
  95.        if(strcmp(previous->word,newWordNode->word) == 0)
  96.        {
  97.           printf("POSITION HERE\n");
  98.           curPos = current->posHead;
  99.           while(curPos != NULL)
  100.           {
  101.              prevPos = curPos;
  102.              curPos = curPos->next;
  103.           }
  104.           if(prevPos == NULL)
  105.           {
  106.              index->wordCount++;
  107.              newPositionNode->positionNum = index->wordCount;
  108.              newPositionNode->next = current->posHead;
  109.              current->posHead = newPositionNode;
  110.              return;
  111.           }
  112.           else
  113.           {
  114.              index->wordCount++;
  115.              newPositionNode->positionNum = index->wordCount;
  116.              prevPos -> next = newPositionNode;
  117.              newPositionNode->next = curPos;
  118.              return;
  119.           }
  120.          return;  
  121.        }
  122.  
  123.    }
  124.  
  125.  
  126.    if(previous ==NULL)
  127.    {
  128.       newWordNode->next = index->head;
  129.       index->head = newWordNode;
  130.    }
  131.    else
  132.    {
  133.       previous -> next = newWordNode;;
  134.       newWordNode->next = current;
  135.       index->wordCount++;
  136.  
  137.       newPositionNode->positionNum = index->wordCount;
  138.       previous->posHead = newPositionNode;
  139.       printf("positionNodeNum = %d\n", newPositionNode->positionNum);
  140.    }
  141.  
  142.    current = index -> head;
  143.  
  144.    while(current != NULL)
  145.    {
  146.       printf("%s n Ascii %i\n",current->word,current->word);
  147.       current = current-> next;
  148.    }
  149.    printf("%d\n",index->wordCount);
  150.  
  151.  
  152. }
  153.  
  154. /*HEADER FILE IndexADT.h*/
  155. #include <stdio.h>
  156. #include <stdlib.h>
  157. #include <string.h>
  158.  
  159.  
  160. typedef struct positionNode
  161. {
  162.    int positionNum;
  163.    struct positionNode* next;
  164. }PosNode;
  165.  
  166. typedef struct wordNode
  167. {
  168.    char word[35];
  169.    int wordNum;
  170.    struct wordNode* next;
  171.    PosNode* posHead;
  172. }WordNode;
  173.  
  174. typedef struct wordList
  175. {
  176.    WordNode* head;
  177.    int wordCount;
  178. }IndexADT;
  179.  
  180. int IndexInit(IndexADT*);
  181. int IndexCreate(IndexADT*, char*);
  182. int IndexDisplay(IndexADT*);
  183. int IndexSearch(IndexADT*, char*);
  184. int IndexRecompose(IndexADT*);
  185. int IndexClose(IndexADT*, char*);
  186.  
  187. void addToADT(IndexADT*,char*);
  188.  
  189. /* assign1.c */
  190. #include "IndexADT.h"
  191.  
  192. int main(int argc,char** argv)
  193. {
  194.    IndexADT worldList;
  195.  
  196.    IndexInit(&worldList);
  197.  
  198.    if(argc != 2)
  199.    {
  200.       fprintf(stderr, "Invalid usage: %s \n", argv[0]);
  201.       exit(1);
  202.    }
  203.  
  204.    IndexCreate(&worldList,argv[1]);
  205.  
  206.   return EXIT_SUCCESS;
  207. }
  208.  
To compile the file:
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
Sep 5 '06 #1
  • viewed: 1901
Share:

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Andr? Roberge | last post: by
16 posts views Thread by qwweeeit | last post: by
9 posts views Thread by Lans | last post: by
15 posts views Thread by Enzo | last post: by
14 posts views Thread by nullptr | last post: by
8 posts views Thread by Roman Mashak | last post: by
8 posts views Thread by wastedhello | last post: by
47 posts views Thread by fishpond | last post: by
9 posts views Thread by John Zenger | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.