473,699 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 2180

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

Similar topics

1
2743
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 retained for backward compatibility: tokenize(readline) ====
16
2294
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 embedded comments I used the tokenize module. To my surprise the analysed output is different from the input (the last tuple element should exactly replicate the input line) The error comes out in correspondance of a triple string.
9
18425
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 word char *mystring "Jane and Peter and Tom and Cindy" char *delim = " and "; char *token; token = strtok(mystring, delim);
15
3382
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
9923
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 <string.h>
8
1656
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 conference? Thanks in advance! With best regards, Roman Mashak. E-mail: mrv@tusur.ru
8
1694
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 values are given they will work, but will cause a random segmentation fault. (BUT only with giving two values.) 3. -average doesnt give average. 4. -gta is ment to give all the values greater then the average. but doesnt do anything 5. -gt is ment...
47
3003
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 course the reason for doing this is to save time in case equal pointers are passed to strcmp. But it seems to me that this could create an inconsistency in the degenerate case when s1 points to memory that is not null-terminated, i.e. by some freak...
9
3733
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 will help spell-check literal strings entered into Python source code? I don't mean spell-check strings entered by the user; I mean, go through the .py file, isolate strings, and tell me when the strings contain misspelled words. In an ideal...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.