- #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;
-
}
-
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 0 2180 Sign in to post your reply or Sign up for a free account.
Similar topics |
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)
====
|
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.
|
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);
|
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
|
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>
| |
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
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |