Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LINES 4096
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 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);
}
/* 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 for percentage calculation */
void percentage_calculation_numbers(char* a_pch, int a_numchar, char* a_b)
{
FILE *sp;
FILE *fp;
char *filedata = malloc(300);
char str2[7];
char vowels[] = "aeiouyAEIOUY";
char letters[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
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 */
sp = fopen("readlist.txt", "r");
fp = fopen("writelist.txt", "a+");
if(!sp)
{
perror("Error: file readlist.txt was not found or opened");
exit(1);
}
if((fp = fopen("writelist.txt", "a+"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
printf("%s", a_pch);
/* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
while(a_pch != NULL)
{
/* text file comparison begin */
while(fgets(filedata, 300, sp))
{
if(_memicmp(a_pch, filedata, strlen(a_pch)) == 0
&& (filedata[strlen(a_pch)] == ' '
|| filedata[strlen(a_pch)] == '\n'))
{
/* adding numchar to filedata erases the word and leaves the grammar intact */
printf("%s\n", filedata);
/* write the text file comparison result to file */
fprintf(fp, "%s\n", filedata);
}
}
/* text file comparison end */
/* identify the first and last letter in the word begin */
red = find_letter(str2, a_pch);
if(strpbrk(str2, letters))
{
one++;
}
if(strpbrk(str2, vowels))
{
two++;
}
reverse = rev(a_pch);
red = find_letter(str2, a_pch);
if(strpbrk(str2, letters))
{
three++;
}
if(strpbrk(str2, vowels))
{
four++;
}
/* identify the first and last letter in the word end */
/* The math to see what kind of word it is start */
if(total = (one && three))
{
five++;
}
else if(total = (one && four))
{
six++;
}
else if(total = (two && three))
{
seven++;
}
else if(total = (two && four))
{
eight++;
}
/* The math to see what kind of word it is end */
/* Reset the pointers */
one = 0;
two = 0;
three = 0;
four = 0;
total = 0;
rewind(sp);
a_pch = strtok (NULL, " .");
}
/* pch is NULL, so I find alphabet in b and pass that into pch */
a_pch = strpbrk(a_b, alphabet);
/* 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);
/* 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);
printf("\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n", percentage_1, percentage_2, percentage_3, percentage_4);
/* 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\n", percentage_1, percentage_2, percentage_3, percentage_4);
fclose(fp);
fclose(sp);
}
/* function so I don't have to type getch all over the place */
void MyExit(void) { system("pause"); }
/* the main program */
int numchar;
int main ()
{
/* declaring and initiaizing variables */
FILE *book;
char * pch = malloc(300);
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char buffer[LINES];
atexit(MyExit);
/* create text file */
split_by_sentence();
/* open text file or report error */
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-=[]\'\"/;/|,./{}:<>?");
if(strpbrk(pch, alphabet))
{
/* numchar counts the numbers of characters in the pch string */
numchar = countchar (pch);
/* I calculate the percentage */
percentage_calculation_numbers(pch, numchar, buffer);
}
else
{
continue;
}
}
fclose(book);
return 0;
}
Here is my text files: