Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LINES 4096
char *Camestros_1()
{
char *Camestros_1 = "point\nautofire\npoint\nshoot\nreload\nreload\npoint";
return Camestros_1;
}
char red;
char* reverse;
/* The function to split the input into a sentence per line */
int split_by_sentence(void)
{
int character, file_character=0;
char buffer[1024];
FILE *book=fopen("readtext1.txt", "r");
FILE *book2=fopen("readtext.txt", "a+");
if(!book) {printf("Error: unable to open input file!\n"); return 1;}
if(!book2) {printf("Error: unable to open output file!\n"); return 1;}
while(file_character!=EOF)
{
buffer[0]='\0';
for(character=0;character<sizeof(buffer);character++)
{
file_character=fgetc(book);
if(file_character==EOF)
break;
if(file_character=='.')
{
buffer[character]='\0';
break;
}
if(file_character=='?')
{
buffer[character]='\0';
break;
}
if(file_character=='!')
{
buffer[character]='\0';
break;
}
buffer[character]=file_character;
}
if(file_character==EOF)
break;
fprintf(book2, "%s.\n", buffer);
}
fclose(book);
fclose(book2);
putchar('\n');
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 )
{
char temp = str[ start ];
str[ start ] = str[ end ];
str[ end ] = temp;
++start;
--end;
}
return str;
}
/* The function for percentage calculation */
void percentage_calculation_numbers(char* a_pch, char* a_b)
{
FILE *fp;
char str2[7];
char period[] = ".";
char vowels[] = "aeiouyAEIOUY";
char letters[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char sentence[4096] = {0};
float one;
float two;
float three;
float four;
float total;
float five;
float six;
float seven;
float eight;
float total_2;
float percentage_1;
float percentage_2;
float percentage_3;
float percentage_4;
one = 0;
two = 0;
three = 0;
four = 0;
total = 0;
five = 0;
six = 0;
seven = 0;
eight = 0;
total_2 = 0;
percentage_1 = 0;
percentage_2 = 0;
percentage_3 = 0;
percentage_4 = 0;
/* open text file or report error */
fp = fopen("writelist.txt", "a+");
if(!fp) {printf("Error: unable to open input file!\n");}
/* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
while(a_pch != NULL)
{
strcat(sentence, a_pch);
strcat(sentence, " ");
/* identify the first letter in the word begin */
red = find_letter(str2, a_pch);
if(strpbrk(str2, letters))
{
one++;
}
if(strpbrk(str2, vowels))
{
two++;
}
/* identify the first letter in the word end */
/* identify the last letter in the word begin */
reverse = rev(a_pch);
red = find_letter(str2, a_pch);
if(strpbrk(str2, letters))
{
three++;
}
if(strpbrk(str2, vowels))
{
four++;
}
/* identify the last letter in the word end */
/* The math using the first and last letter begin */
/* pch is backwards, so I use rev to reverse it back */
/*printf("%s uncomment to test to see if the string is backwards", pch);*/
reverse = rev(a_pch);
/*The math using the first and last letter begin
Here I give the word a new string value, based on how the vowel is situated in the word.*/
if(total = (one && three))
{
five++;
fprintf(fp, "point\n", a_pch);
}
else if(total = (one && four))
{
six++;
fprintf(fp, "shoot\n", a_pch);
}
else if(total = (two && three))
{
seven++;
fprintf(fp, "reload\n", a_pch);
}
else if(total = (two && four))
{
eight++;
fprintf(fp, "autofire\n", a_pch);
}
/* The math using the first and last letter end */
/* Reset the pointers */
one = 0;
two = 0;
three = 0;
four = 0;
total = 0;
a_pch = strtok (NULL, " .");
}
/* Add separation between lines in console window, to identify sentence separation*/
putchar('\n');
/* pch is NULL, so I find alphabet in b and pass that into pch */
a_pch = strpbrk(a_b, alphabet);
/* Count the pointers */
total_2 = (five + six + seven + eight);
percentage_1 = ((five)/ total_2);
percentage_2 = ((six)/ total_2);
percentage_3 = ((seven)/ total_2);
percentage_4 = ((eight)/ total_2);
/* puts a period into the textfile after a sentence is processed
This way I can count the number of periods in the text file */
fprintf(fp, ".\n");
fclose(fp);
/* Displays the sentence above the list.*/
printf("%s %s", sentence, period);
memset(&sentence[0], 0, sizeof(sentence));
}
/* function to find pattern, useful to identify how the words parallel a syllogism */
int find_pattern(void)
{
/* declare variable arrays */
int period = 0;
int one = 1;
char buffer[LINES];
char pch[LINES] = {0};
/* Read the list made by the percentage function */
FILE *book=fopen("writelist.txt", "r");
if(!book) {printf("Error: unable to open input file!\n"); return 1;}
while(fgets(buffer, sizeof(buffer), book)!=NULL)
{
/* Find the word then add it the the pch string */
if (strstr(buffer, "point"))
{
strcat(pch, buffer);
}
if (strstr(buffer, "shoot"))
{
strcat(pch, buffer);
}
if (strstr(buffer, "reload"))
{
strcat(pch, buffer);
}
if (strstr(buffer, "autofire"))
{
strcat(pch, buffer);
}
/* counting the periods in the input text file*/
if (strstr(buffer, "."))
{
period++;
}
/* After two periods are counted the while loop is exited */
if(period > one)
{
if(strstr(pch, Camestros_1()))
{
printf("\nType Camestros (AEO-2) Syllogism pattern found\nP are M\nNo S is M\n\n");
}
//////////////////////////////////////////////////////////////////////////////////
/* resetting the pch array to zero, so there is room to fill it again */
if(period > one)
{
memset(&pch[0], 0, sizeof(pch));
}
period = 0;
}
}
fclose(book);
return 0;
}
/* 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 *book;
char * pch = malloc(300);
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer[LINES];
/* The function is called so I don't have to type getch */
atexit(MyExit);
/* The function is called and creates the text file */
split_by_sentence();
/* The text file just created is opened */
book = fopen("readtext.txt", "r");
if(!book)
{
perror("Error: file readtext.txt was not found or opened");
return 0;
}
/* read from file */
while(fgets(buffer, sizeof(buffer), book)!=NULL)
{
/* I tokenize the input string into individual words */
pch = strtok (buffer, " ~!@#$%^&*()_+`1234567890-=[]\'\"/;/|,./{}:<>?");
/* I find the alphabet in pch after it's run through strtok */
if(strpbrk(pch, alphabet))
{
/* I run the percentage function which also creates the new terms for the words processed */
percentage_calculation_numbers(pch, buffer);
}
}
fclose(book);
find_pattern();
return 0;
}
It works. I will now convert my variable arrays to functions and call them in the if statements as seen in the code.