I can process a string input, but not get a result for every sentence in the string.
Here is the code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* function prototype to count the number of characters in a string */
int countchar (char []);
/* function prototype to find the first and last letter in the word */
char find_letter (char* a, char* b);
char red;
/* function prototype to reverse the letters in the word */
char* rev(char* str);
char* reverse;
/* function so I don't have to type getch all over the place */
void MyExit(void) { system("pause"); }
/* the main program */
int main ()
{
/* declaring and initiaizing variables */
FILE *sp;
FILE *fp;
char *filedata = malloc(300);
char str2[7];
char * pch = malloc(300);
char vowels[] = "aeiouy";
char letters[] = "bcdfghjklmnpqrstvwxz";
char *b = malloc(sizeof(char)*1000);
float one, *A;
float two, *B;
float three, *C;
float four, *D;
float total, *E;
float five, *F;
float six, *G;
float seven, *H;
float eight, *I;
float total_2, *J;
float percentage_1, *K;
float percentage_2, *L;
float percentage_3, *M;
float percentage_4, *N;
int numchar;
A = &one;
B = &two;
C = &three;
D = &four;
E = &total;
*A = 0;
*B = 0;
*C = 0;
*D = 0;
*E = 0;
F = &five;
G = &six;
H = &seven;
I = &eight;
J = &total_2;
K = &percentage_1;
L = &percentage_2;
M = &percentage_3;
N = &percentage_4;
*F = 0;
*G = 0;
*H = 0;
*I = 0;
*J = 0;
*K = 0;
*L = 0;
*M = 0;
*N = 0;
atexit(MyExit);
/* open text file or report error */
sp = fopen("readlist.txt", "r");
fp = fopen("writelist.txt", "w");
if(!sp)
{
perror("Error: file readlist.txt was not found or opened");
return 0;
}
if((fp = fopen("writelist.txt", "w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
/* input sentence */
printf("Input a short sentence: ");
gets(b);
pch = strtok (b, " ,.-");
/* numchar counts the numbers of characters in the pch string */
numchar = countchar (pch);
while(pch != NULL)
{
/* text file comparison begin */
while(fgets(filedata, 300, sp))
{
if(_memicmp(pch, filedata, strlen(pch)) == 0
&& (filedata[strlen(pch)] == ' '
|| filedata[strlen(pch)] == '\n'))
{
/* adding numchar to filedata erases the word and leaves the grammar intact */
printf("%s\n%s\n", pch, filedata+numchar+1);
/* write the text file comparison result to file */
fprintf(fp, "%s\n%s\n", pch, filedata+numchar+1);
break;
}
}
/* text file comparison end */
/* identify the first and last letter in the word begin */
red = find_letter(str2, pch);
if(strpbrk(str2, letters))
{
(*A)++;
}
if(strpbrk(str2, vowels))
{
(*B)++;
}
reverse = rev(pch);
red = find_letter(str2, pch);
if(strpbrk(str2, letters))
{
(*C)++;
}
if(strpbrk(str2, vowels))
{
(*D)++;
}
/* identify the first and last letter in the word end */
/* The math to see what kind of word it is start */
if(*E = (*A && *C))
{
(*F)++;
}
else if(*E = (*A && *D))
{
(*G)++;
}
else if(*E = (*B && *C))
{
(*H)++;
}
else if(*E = (*B && *D))
{
(*I)++;
}
/* The math to see what kind of word it is start */
/* Reset the pointers */
*A = 0;
*B = 0;
*C = 0;
*D = 0;
/* print result of text file comparison */
rewind(sp);
pch = strtok (NULL, " ,.-");
}
/* count the pointers */
*J = (*F + *G + *H + *I);
*K = ((*F)/ *J);
*L = ((*G)/ *J);
*M = ((*H)/ *J);
*N = ((*I)/ *J);
printf("\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n", *K, *L, *M, *N);
/* write the pointers result to file */
fprintf(fp, "\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n", *K, *L, *M, *N);
fclose(fp);
return 0;
}
/* The function to find the first letter in the word */
char find_letter (char* a, char* b)
{
char string = strlen(b);
strncpy (a,b,1);
a[1]='\0';
return 0;
}
/* The function to reverse the characters in a string */
char* rev(char* str)
{
int end= strlen(str)-1;
int start = 0;
while( start<end )
{
str[start] ^= str[end];
str[end] ^= str[start];
str[start]^= str[end];
++start;
--end;
}
return str;
}
/* The function to count the number of characters in a string */
int countchar (char list[])
{
int i, count = 0;
for (i = 0; list[i] != '\0'; i++)
count++;
return (count);
}
Here is the contents of the readlist.txt file:
Code:
car n adj /*I know car is not a adjective*/
ran v
And here is the results:
Code:
Input a short sentence: car ran
car
n adj
ran
v
letter vowel letter 1.000000
letter vowel vowel 0.000000
vowel vowel letter 0.000000
vowel vowel vowel 0.000000
Press any key to continue . . .
Now I want to input a book from one copy and paste but that would give one result. I want a result per sentence and I have no idea on how to do this? Can you tell me how, please? :wink:
Looking at the results:
Code:
Input a short sentence: car ran
car
n adj
ran
v
letter vowel letter 1.000000
letter vowel vowel 0.000000
vowel vowel letter 0.000000
vowel vowel vowel 0.000000
Press any key to continue . . .
You see car has a definition as a adjective and a noun, but words change grammar depending on how they are placed in the sentence.
This code create the word and possible grammar in a sentence, next I can create code to filter out the adj from the result and just have
'car
noun'.
But that's after I get this mess I'm in sorted out. If I can get it sorted out that is. :confused: