473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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
0 2164

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

Similar topics

1
by: Andr? Roberge | last post by:
According to the Python documentation: 18.5 tokenize -- Tokenizer for Python source .... The primary entry point is a generator: generate_tokens(readline) .... An older entry point is...
16
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the...
9
by: Lans | last post by:
I have a string that I need to tokenize but I need to use a string token see example i am trying the following but strtok only uses characters as delimiters and I need to seperate bu a certain...
15
by: Enzo | last post by:
Hi Ng, It's possible to protect the source code of a js file? With PHP? Thanks in advance! Enzo
14
by: nullptr | last post by:
Hi, As an exercise, I've written a program implementing run-length encoding. I would be more that happy to hear your comments, suggestions, etc Thanks, --- #include <stdio.h> #include...
8
by: Roman Mashak | last post by:
Hello, All! I started to implement simple command line interface (CLI), and wanna know how to use table of fucntion calls. Could you please, recommend me some link or give example directly in...
8
by: wastedhello | last post by:
ok. here are a list of things i cant fix. ive been trying for way to long now. 1. if more then 1000 values are told, will still continue with program. 2. if -max, min, average etc run, and 2...
47
by: fishpond | last post by:
One way I've seen strcmp(char *s1, char *s2) implemented is: return immediately if s1==s2 (equality of pointers); otherwise do the usual thing of searching through the memory at s1 and s2. Of...
9
by: John Zenger | last post by:
To my horror, someone pointed out to me yesterday that a web app I wrote has been prominently displaying a misspelled word. The word was buried in my code. Is there a utility out there that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.