I now have a complete program for modifying words.
Here is the code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h> // ***
/* Removes the need to add a getch() before every return or exit statement in the program */
void MyExit(void) { system("pause"); } // ***
int main ()
{
/* Declare variables */
char *b = malloc(sizeof(char)*1000);
char *filedata = malloc(300);
char *filedata_2 = malloc(300);
char * pch;
FILE * fp;
FILE * sp;
atexit(MyExit); // ***
/* Fill the fp variable with a text file and read the text file */
fp = fopen("worklist.txt", "r");
sp = fopen("worklist_2.txt", "r");
/* if the file was not opened */
if(!fp)
{
perror("Error: file worklist.txt was not found or opened");
return 0;
}
if(!sp)
{
perror("Error: file worklist_2.txt was not found or opened");
return 0;
}
/* Input a sentence and fill the b variable */
printf("Input a short sentence: ");
gets(b);
/* Take the b variable and split it into a word per line to fill the pch variable */
pch = strtok (b," ,.-");
/* read the pch variable until it is empty */
while (pch != NULL)
{
/* Position the files to the start.
Otherwise the fgets will start reading from the last position when processing the previous word. */
rewind (fp);
/* reads the worklist file and writes the word to the filedata_2 variable. */
if(fgets(filedata_2, 300, sp))
{
/* Uses the pch data in strlen, to compared the words in the pch and filedata_2 variables, to see if they are the same. */
if(memcmp(pch, filedata_2, strlen(pch)) == 0)
/* Position the files to the start.
Otherwise the fgets will start reading from the last position when processing the previous word. */
rewind (sp);
{
/* reads the worklist file and writes the word to the filedata variable. */
while ((fgets(filedata, 300, fp)))
{
/* Uses the pch data in strlen, to compared the words in the pch and filedata variables, to see if they are the same. */
if(memcmp(pch, filedata, strlen(pch)) == 0) break;
else strcpy(filedata, "Not a match!");
}
/* If the data in pch and filedata are the same then print the data in filedata */
printf("%s\n",filedata);
}
}
/* Prevents the result from repeating endlessly */
pch = strtok (NULL, " ,.-");
}
return 0;
}
You need two text files. One text file has the word with no 'n' or 'adj' etc next to them, just the word itself. One text file has the word and the 'n' or 'adj' beside it.
Here is my text file contents;
Code:
worklist_2.txt
The
dog
doggy
worklist.txt
The adj
dog n
doggy n
Here is the results of me running the program and entering the words 'The dog':
Code:
Input a short sentence: The dog
The adj
dog n
Press any key to continue . . .
So now the basic program is finished and now I wonder what to do with it now. I posted this here because you helped me before and now I have the finished version i wanted to show it to you.
I especially wanted to show it to Adak, so here you go.