Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define STRING 4096
#define MAX_STRING 4096
#define COPY_WHOLE_CONSONANT_WORD 4096
char red;
/* The function to split the input into a sentence per line */
int split_by_sentence(void)
{
FILE *book;
FILE *book2;
int character, file_character=0;
char buffer[1024];
char mystring[STRING] = {0};
book = fopen("readtext1.txt", "w");
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;}
/* input a sentence, then save to mystring variable, then write to file. */
printf("Input a sentence. Press Enter when done.\n");
fgets(mystring, 4096, stdin);
fprintf(book, "%s", mystring);
fclose(book);
/* Now read the file and split into sentence per line. */
book=fopen("readtext1.txt", "r");
while(file_character!=EOF)
{
buffer[0]='\0';
for(character=0;character<sizeof(buffer);character++)
{
file_character=fgetc(book);
if ( (file_character == EOF) || (file_character == '.') || (file_character == '?') || (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');
memset(&mystring[0], 0, sizeof(mystring));
return 0;
}
void delete_char(char *src, char n, int len)
{
char *dst;
int i;
// Do not remove NULL characters.
if ( n == 0 )
return;
// Small attempt to control a buffer overflow if the
// the string is not null-terminated and a proper length
// is not specified.
if ( len <= 0 )
len = MAX_STRING;
dst = src;
for ( i = 0; i < len && *src != 0; i++, src++ )
{
if ( *src != n )
*dst++ = *src;
}
// Ensure the string is null-terminated.
*dst = 0;
return;
}
/* 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 *reverse(char *s)
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
return 0;
}
void percentage_calculation(char* a_pch)
{
FILE *fp;
char vowels[] = "aeiouyAEIOUY";
char letters[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
char * pch = malloc(300);
char str2[7] = {0};
char buf1[MAX_STRING];
char byte [4] = {0};
int sentence_count = 0;
int i = 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", "w");
if(!fp) {printf("Error: unable to open input file!\n");}
/* Tokenize the input for the while loop */
strncpy(buf1, a_pch, sizeof(buf1));
for ( i = 0 ; dels[i] != '\0' ; i++ ) {
delete_char(buf1, dels[i], 0);
}
pch = strtok (buf1, " ");
/* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
while(pch != NULL)
{
/*printf("test %s\n", pch);*/
/* identify the first letter in the word begin */
red = find_letter(str2, 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 */
if(a_pch[strlen(pch)-1] != ' ')
byte[0] = pch[strlen(pch)-1];
else
byte[0] = pch[strlen(pch)-2];
find_letter(str2, byte);
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 */
/*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++;
}
else if(total = (one && four))
{
six++;
}
else if(total = (two && three))
{
seven++;
}
else if(total = (two && four))
{
eight++;
}
/* The math using the first and last letter end */
/* Reset the pointers */
one = 0;
two = 0;
three = 0;
four = 0;
total = 0;
memset(&byte[0], 0, sizeof(byte));
pch = strtok (NULL, " ");
}
/* 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 */
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);
free(pch);
i = 0;
memset(&buf1[0], 0, sizeof(buf1));
memset(&str2[0], 0, sizeof(str2));
}
void redefined_sentence (char* sentence)
{
FILE *list = NULL;
FILE *last_word = NULL;
FILE *intermediate = NULL;
FILE *comparison = NULL;
FILE *consonant_words = NULL;
char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
char mystring[STRING] = {0};
char copy_whole_consonant_word[COPY_WHOLE_CONSONANT_WORD] = {0};
char * pch = malloc(4096);
char * list_of_words = malloc(4096);
char * last = malloc(4096);
char * letters = malloc(4096);
char * whole_consonant_words = malloc(4096);
char * last_string = malloc(4096);
char * words = malloc(4096);
char * one_word = malloc(4096);
char * calculation = malloc(4096);
int loop = 0;
int loop_2 = 0;
int i = 0;
int count = 0;
list = fopen("list.txt", "w");
if(list == NULL)
{
perror("Error: file list.txt was not found or opened");
goto Cleanup;
}
last_word = fopen("last_word.txt", "w");
if(last_word == NULL)
{
perror("Error: file last_word.txt was not found or opened");
goto Cleanup;
}
consonant_words = fopen("consonant_words.txt", "w");
if(consonant_words == NULL)
{
perror("Error: file consonant_words.txt was not found or opened");
goto Cleanup;
}
comparison = fopen("comparison.txt", "w");
if(comparison == NULL)
{
perror("Error: file comparison.txt was not found or opened");
goto Cleanup;
}
for ( i = 0 ; dels[i] != '\0' ; i++ ) {
delete_char(sentence, dels[i], 0);
}
/* split sentence into a word per line, and save to list.txt */
pch = strtok(sentence, " ");
while(pch != NULL)
{
fprintf(list, "%s\n", pch);
pch = strtok (NULL, " ");
}
free(pch);
fclose(list);
/* find consonants in the list.txt, write results to intermediate.txt */
list = fopen("list.txt", "r");
if(list == NULL)
{
perror("Error: file list.txt was not found or opened");
goto Cleanup;
}
intermediate = fopen("intermediate.txt", "w");
if(intermediate == NULL)
{
perror("Error: file intermediate.txt was not found or opened");
goto Cleanup;
}
while(fgets(list_of_words, 4096, list)!=NULL)
{
fprintf(intermediate, "\n");
for(loop=0;list_of_words[loop]!='\0';loop++)
{
for(loop_2=0;consonant[loop_2]!='\0';loop_2++)
{
if(list_of_words[loop] == consonant[loop_2])
{
fprintf(intermediate, "%c", consonant[loop_2]);
count++;
}
}
}
if(count > 0)
{
fprintf(consonant_words, "%s", list_of_words);
count = 0;
}
}
free(list_of_words);
fclose(list);
fclose(intermediate);
fclose(consonant_words);
loop = 0;
loop_2 = 0;
/* open intermediate.txt, and put the consonants from the last word in the text file into a variable */
intermediate = fopen("intermediate.txt", "r");
if(intermediate == NULL)
{
perror("Error: file intermediate.txt was not found or opened");
goto Cleanup;
}
while(fgets(last, 4096, intermediate)!=NULL)
{
}
fclose(intermediate);
/* compare the consonants from the last word to the consonants from the rest of the words.
Also loop through file with the whole word version.
Then, display the results on the screen. */
intermediate = fopen("intermediate.txt", "r");
if(intermediate == NULL)
{
perror("Error: file intermediate.txt was not found or opened");
goto Cleanup;
}
consonant_words = fopen("consonant_words.txt", "r");
if(consonant_words == NULL)
{
perror("Error: file consonant_words.txt was not found or opened");
goto Cleanup;
}
while(fgets(letters, 4096, intermediate)!=NULL)
{
if(strpbrk(letters, consonant))
{
fgets(whole_consonant_words, 4096, consonant_words);
if(strpbrk(letters, last))
{
fprintf(last_word, "%s", whole_consonant_words);
memmove(copy_whole_consonant_word, whole_consonant_words, strlen(whole_consonant_words)+1);
delete_char(copy_whole_consonant_word, '\n', 0);
fprintf(comparison, "%s ", copy_whole_consonant_word);
memset(©_whole_consonant_word[0], 0, sizeof(copy_whole_consonant_word));
count++;
}
}
}
fprintf(comparison, "\n");
free(last);
free(whole_consonant_words);
free(letters);
fclose(comparison);
fclose(intermediate);
fclose(consonant_words);
fclose(last_word);
//////////////
comparison = fopen("comparison.txt", "r");
if(comparison == NULL)
{
perror("Error: file comparison.txt was not found or opened");
goto Cleanup;
}
while(fgets(words, 4096, comparison)!=NULL)
{
printf("%s", words);
}
fclose(comparison);
free(words);
last_word = fopen("last_word.txt", "r");
if(last_word == NULL)
{
perror("Error: file last_word.txt was not found or opened");
goto Cleanup;
}
while(fgets(last_string, 4096, last_word)!=NULL)
{
}
printf("The last word is: %s", last_string);
fclose(last_word);
free(last_string);
comparison = fopen("comparison.txt", "r");
if(comparison == NULL)
{
perror("Error: file comparison.txt was not found or opened");
goto Cleanup;
}
while(fgets(calculation, 4096, comparison)!=NULL)
{
percentage_calculation(calculation);
}
fclose(comparison);
free(calculation);
//////////////
Cleanup:
if (list != NULL)
{
fclose(list);
list = NULL;
}
if (intermediate != NULL)
{
fclose(intermediate);
intermediate = NULL;
}
if (comparison != NULL)
{
fclose(comparison);
comparison = NULL;
}
if (consonant_words != NULL)
{
fclose(consonant_words);
consonant_words = NULL;
}
if (last_word != NULL)
{
fclose(last_word);
last_word = NULL;
}
loop = 0;
loop_2 = 0;
i = 0;
memset(&mystring[0], 0, sizeof(mystring));
}
void MyExit(void) { system("pause"); }
int main ()
{
FILE *book;
FILE *comparison;
char * read_book = malloc(4096);
atexit(MyExit);
split_by_sentence();
book=fopen("readtext.txt", "r");
while(fgets(read_book, 4096, book)!=NULL)
{
redefined_sentence(read_book);
}
free(read_book);
fclose(book);
book=fopen("readtext.txt", "w");
while(fgets(read_book, 4096, book)!=NULL)
{
fprintf(book, "");
}
free(read_book);
fclose(book);
return 0;
}
Here's an example showing how the percentages work, look at the letters or vowels on the ends of the word: