-
Here is my newest code:
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
#define CELARENT_WORD_FIVE 4096
#define CF_WORD_TWO 4096
#define BUF 4096
#define C_PLUS 4096
#define C_PLUS_PLUS 4096
#define D_PLUS 4096
#define D_PLUS_PLUS 4096
char red;
/* The function to delete unwanted chars from strings */
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 search text file for compatible word */
void search(char *src, char *a, char *b, char *c)
{
FILE *sp;
FILE *read_bad;
FILE *write;
char byte [4] = {0};
char bit [4] = {0};
char c_plus[C_PLUS] = {0};
char c_plus_plus[C_PLUS_PLUS] = {0};
char d_plus[D_PLUS] = {0};
char d_plus_plus[D_PLUS_PLUS] = {0};
char *filedata = malloc(300);
char *c_c = malloc(300);
char *_d = malloc(300);
char *d_d = malloc(300);
char *list_of_bad = malloc(300);
int _c = 0;
int cc = 0;
int d = 0;
int dd = 0;
int number = 0;
int loop = 0;
sp = fopen("readfile.txt", "r");
if(sp == NULL)
{
perror("Error: file readfile.txt was not found or opened");
goto Cleanup;
}
write = fopen("m_and_s.txt", "w");
if(write == NULL)
{
perror("Error: file m_and_s.txt was not found or opened");
goto Cleanup;
}
read_bad = fopen("bad.txt", "r");
if(read_bad == NULL)
{
perror("Error: file bad.txt was not found or opened");
goto Cleanup;
}
delete_char(c, '\n', 0);
while(fgets(filedata, 300, sp)!=NULL)
{
bit[0] = filedata[0];
byte[0] = filedata[strlen(filedata)-2];
// The for loop breaks down the word so the next if statement can find the same letter used twice.
// I find that if the same letter if used twice the result is acceptable.
for(loop=0;filedata[loop]!='\0';loop++)
{
if(filedata[loop] == filedata[loop+1])
{
// I look for a in the word, and b in that exact location.
if( (strpbrk(filedata, a)) && (!strncmp (byte, b, 1)) || (!strncmp (bit, b, 1)) )
{
/* Save the result from this number for later use */
number++;
if(number==13)
strcpy(c_c, filedata);
if(number==16)
strcpy(_d, filedata);
if(number==30)
strcpy(d_d, filedata);
/* create a sentence to compare to the sentence in the bad.txt */
if(number==45)
{
memmove(c_plus, c, strlen(c)+1);
strcat(c_plus, " ");
strcat(c_plus, filedata);
memmove(c_plus_plus, c, strlen(c)+1);
strcat(c_plus_plus, " ");
strcat(c_plus_plus, c_c);
memmove(d_plus, c, strlen(c)+1);
strcat(d_plus, " ");
strcat(d_plus, _d);
memmove(d_plus_plus, c, strlen(c)+1);
strcat(d_plus_plus, " ");
strcat(d_plus_plus, d_d);
/* The bad.txt is opened and compared to the sentence made in the previous step */
while(fgets(list_of_bad, 4096, read_bad)!=NULL)
{
if(!strcoll(c_plus, list_of_bad))
{
_c++;
}
else if(!strcoll(c_plus_plus, list_of_bad))
{
cc++;
}
else if(!strcoll(d_plus, list_of_bad))
{
d++;
}
else if(!strcoll(d_plus_plus, list_of_bad))
{
dd++;
}
}
printf("Using the last word, these are four possible word associations:\n\n");
printf("%s\n", filedata);
printf("%s\n", d_d);
printf("%s\n", _d);
printf("%s\n", c_c);
/* If the above didn't add, then a result will be sent back to the sentence generator and written to file. */
if(_c == 0)
{
strcat(src, filedata);
/*printf("%s\n", filedata);*/
/*fprintf(write, "%s %s\n", c, filedata);*/
loop = 0;
memset(&byte[0], 0, sizeof(byte));
memset(&bit[0], 0, sizeof(bit));
memset(&c_plus[0], 0, sizeof(c_plus));
memset(&c_plus_plus[0], 0, sizeof(c_plus_plus));
memset(&d_plus[0], 0, sizeof(d_plus));
memset(&d_plus_plus[0], 0, sizeof(d_plus));
}
else if(dd == 0)
{
strcat(src, d_d);
/*printf("%s\n", d_d);*/
/*fprintf(write, "%s %s\n", c, d_d);*/
loop = 0;
memset(&byte[0], 0, sizeof(byte));
memset(&bit[0], 0, sizeof(bit));
memset(&c_plus[0], 0, sizeof(c_plus));
memset(&c_plus_plus[0], 0, sizeof(c_plus_plus));
memset(&d_plus[0], 0, sizeof(d_plus));
memset(&d_plus_plus[0], 0, sizeof(d_plus));
}
else if(d == 0)
{
strcat(src, _d);
/*printf("%s\n", _d);*/
/*fprintf(write, "%s %s\n", c, _d);*/
loop = 0;
memset(&byte[0], 0, sizeof(byte));
memset(&bit[0], 0, sizeof(bit));
memset(&c_plus[0], 0, sizeof(c_plus));
memset(&c_plus_plus[0], 0, sizeof(c_plus_plus));
memset(&d_plus[0], 0, sizeof(d_plus));
memset(&d_plus_plus[0], 0, sizeof(d_plus));
}
else if(cc == 0)
{
strcat(src, c_c);
/*printf("%s\n", c_c);*/
/*fprintf(write, "%s %s\n", c, c_c);*/
loop = 0;
memset(&byte[0], 0, sizeof(byte));
memset(&bit[0], 0, sizeof(bit));
memset(&c_plus[0], 0, sizeof(c_plus));
memset(&c_plus_plus[0], 0, sizeof(c_plus_plus));
memset(&d_plus[0], 0, sizeof(d_plus));
memset(&d_plus_plus[0], 0, sizeof(d_plus));
}
}
}
}
}
}
fclose(read_bad);
fclose(write);
fclose(sp);
free(list_of_bad);
free(d_d);
free(_d);
free(c_c);
free(filedata);
Cleanup:
if (read_bad != NULL)
{
fclose(read_bad);
read_bad = NULL;
}
if (write != NULL)
{
fclose(write);
write = NULL;
}
if (sp != NULL)
{
fclose(sp);
sp = NULL;
}
return;
}
/* the word generator function: part one */
void word_generator(char* C_F_two)
{
// These arrays are used in fprintf at the bottom of the program
char Celarent_five[CELARENT_WORD_FIVE] = {0};
// These arrays are used for the program to run
char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
char buf1[MAX_STRING];
/* I check the word from the while loop to see what char values it has, so these values are passed to the search function.
The search function then returns a word value from a list of words.*/
/* ________________________________________ */
if(strpbrk(C_F_two, "a"))
{
if(strpbrk(C_F_two, "b"))
{
search(buf1, "a", "b", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "c"))
{
search(buf1, "a", "c", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "d"))
{
search(buf1, "a", "d", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "f"))
{
search(buf1, "a", "f", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "g"))
{
search(buf1, "a", "g", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "h"))
{
search(buf1, "a", "h", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "j"))
{
search(buf1, "a", "j", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "k"))
{
search(buf1, "a", "k", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "l"))
{
search(buf1, "a", "l", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "m"))
{
search(buf1, "a", "m", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "n"))
{
search(buf1, "a", "n", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "p"))
{
search(buf1, "a", "p", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "q"))
{
search(buf1, "a", "q", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "r"))
{
search(buf1, "a", "r", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "s"))
{
search(buf1, "a", "s", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "t"))
{
search(buf1, "a", "t", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "v"))
{
search(buf1, "a", "v", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "w"))
{
search(buf1, "a", "w", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "x"))
{
search(buf1, "a", "x", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "z"))
{
search(buf1, "a", "z", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
}
else if(strpbrk(C_F_two, "e"))
{
if(strpbrk(C_F_two, "b"))
{
search(buf1, "e", "b", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "c"))
{
search(buf1, "e", "c", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "d"))
{
search(buf1, "e", "d", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "f"))
{
search(buf1, "e", "f", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "g"))
{
search(buf1, "e", "g", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "h"))
{
search(buf1, "e", "h", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "j"))
{
search(buf1, "e", "j", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "k"))
{
search(buf1, "e", "k", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "l"))
{
search(buf1, "e", "l", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "m"))
{
search(buf1, "e", "m", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "n"))
{
search(buf1, "e", "n", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "p"))
{
search(buf1, "e", "p", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "q"))
{
search(buf1, "e", "q", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "r"))
{
search(buf1, "e", "r", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "s"))
{
search(buf1, "e", "s", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "t"))
{
search(buf1, "e", "t", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "v"))
{
search(buf1, "e", "v", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "w"))
{
search(buf1, "e", "w", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "x"))
{
search(buf1, "e", "x", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "z"))
{
search(buf1, "e", "z", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
}
else if(strpbrk(C_F_two, "i"))
{
if(strpbrk(C_F_two, "b"))
{
search(buf1, "i", "b", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "c"))
{
search(buf1, "i", "c", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "d"))
{
search(buf1, "i", "d", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "f"))
{
search(buf1, "i", "f", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "g"))
{
search(buf1, "i", "g", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "h"))
{
search(buf1, "i", "h", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "j"))
{
search(buf1, "i", "j", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "k"))
{
search(buf1, "i", "k", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "l"))
{
search(buf1, "i", "l", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "m"))
{
search(buf1, "i", "m", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "n"))
{
search(buf1, "i", "n", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "p"))
{
search(buf1, "i", "p", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "q"))
{
search(buf1, "i", "q", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "r"))
{
search(buf1, "i", "r", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "s"))
{
search(buf1, "i", "s", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "t"))
{
search(buf1, "i", "t", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "v"))
{
search(buf1, "i", "v", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "w"))
{
search(buf1, "i", "w", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "x"))
{
search(buf1, "i", "x", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "z"))
{
search(buf1, "i", "z", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
}
else if(strpbrk(C_F_two, "o"))
{
if(strpbrk(C_F_two, "b"))
{
search(buf1, "o", "b", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "c"))
{
search(buf1, "o", "c", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "d"))
{
search(buf1, "o", "d", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "f"))
{
search(buf1, "o", "f", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "g"))
{
search(buf1, "o", "g", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "h"))
{
search(buf1, "o", "h", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "j"))
{
search(buf1, "o", "j", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "k"))
{
search(buf1, "o", "k", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "l"))
{
search(buf1, "o", "l", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "m"))
{
search(buf1, "o", "m", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "n"))
{
search(buf1, "o", "n", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "p"))
{
search(buf1, "o", "p", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "q"))
{
search(buf1, "o", "q", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "r"))
{
search(buf1, "o", "r", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "s"))
{
search(buf1, "o", "s", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "t"))
{
search(buf1, "o", "t", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "v"))
{
search(buf1, "o", "v", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "w"))
{
search(buf1, "o", "w", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "x"))
{
search(buf1, "o", "x", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "z"))
{
search(buf1, "o", "z", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
}
else if(strpbrk(C_F_two, "u"))
{
if(strpbrk(C_F_two, "b"))
{
search(buf1, "u", "b", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "c"))
{
search(buf1, "u", "c", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "d"))
{
search(buf1, "u", "d", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "f"))
{
search(buf1, "u", "f", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "g"))
{
search(buf1, "u", "g", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "h"))
{
search(buf1, "u", "h", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "j"))
{
search(buf1, "u", "j", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "k"))
{
search(buf1, "u", "k", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "l"))
{
search(buf1, "u", "l", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "m"))
{
search(buf1, "u", "m", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "n"))
{
search(buf1, "u", "n", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "p"))
{
search(buf1, "u", "p", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "q"))
{
search(buf1, "u", "q", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "r"))
{
search(buf1, "u", "r", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "s"))
{
search(buf1, "u", "s", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "t"))
{
search(buf1, "u", "t", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "v"))
{
search(buf1, "u", "v", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "w"))
{
search(buf1, "u", "w", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "x"))
{
search(buf1, "u", "x", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "z"))
{
search(buf1, "u", "z", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
}
else if(strpbrk(C_F_two, "y"))
{
if(strpbrk(C_F_two, "b"))
{
search(buf1, "y", "b", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "c"))
{
search(buf1, "y", "c", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "d"))
{
search(buf1, "y", "d", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "f"))
{
search(buf1, "y", "f", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "g"))
{
search(buf1, "y", "g", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "h"))
{
search(buf1, "y", "h", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "j"))
{
search(buf1, "y", "j", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "k"))
{
search(buf1, "y", "k", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "l"))
{
search(buf1, "y", "l", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "m"))
{
search(buf1, "y", "m", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "n"))
{
search(buf1, "y", "n", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "p"))
{
search(buf1, "y", "p", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "q"))
{
search(buf1, "y", "q", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "r"))
{
search(buf1, "y", "r", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "s"))
{
search(buf1, "y", "s", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "t"))
{
search(buf1, "y", "t", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "v"))
{
search(buf1, "y", "v", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "w"))
{
search(buf1, "y", "w", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "x"))
{
search(buf1, "y", "x", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
else if(strpbrk(C_F_two, "z"))
{
search(buf1, "y", "z", C_F_two);
strcat(Celarent_five, buf1);
memset(&buf1[0], 0, sizeof(buf1));
}
}
/* ________________________________________ */
/* I get rid of the newline so the sentences look formated, or else the sentences are on different lines */
delete_char(Celarent_five, '\n', 0);
/* I write the results to file */
/* I memset the arrays */
memset(&Celarent_five[0], 0, sizeof(Celarent_five));
memset(&C_F_two[0], 0, sizeof(C_F_two));
return;
}
/* 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;
}
/* 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 == NULL)
{
perror("Error: file writelist.txt was not found or opened");
goto Cleanup;
}
/* 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));
Cleanup:
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
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);
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);
word_generator(last_string);
free(last_string);
//////////////
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 are the results:
Code:
Input a sentence. Press Enter when done.
Keeping up with your gamer gossip? Then you're probably up to snuff on the recen
t Ocean Marketing / Penny-Arcade spat. We held our tongues as the drama unfolded
-- no easy task, considering Engadget's name was dragged into the affair -- but
now it seems like the internet soap-opera is reaching its conclusion.
Keeping up gamer gossip
The last word is: gossip
letter vowel letter 0.750000
letter vowel vowel 0.000000
vowel vowel letter 0.250000
vowel vowel vowel 0.000000
Using the last word, these are four possible word associations:
bookmaking
best-selling
babysitting
approving
probably up to snuff the recent Marketing spat
The last word is: spat
letter vowel letter 0.500000
letter vowel vowel 0.375000
vowel vowel letter 0.125000
vowel vowel vowel 0.000000
Using the last word, these are four possible word associations:
palliation
painfulness
half-asleep
flattop
held tongues as unfolded no easy task considering Engadgets name was into now se
ems like internet soapopera is reaching its conclusion
The last word is: conclusion
letter vowel letter 0.428571
letter vowel vowel 0.190476
vowel vowel letter 0.285714
vowel vowel vowel 0.095238
Using the last word, these are four possible word associations:
callousness
call
caboose
cabbie
Press any key to continue . . .
The first thing it does is get the modified sentence, then it calculates the percent a type of word was used, then it prints possible word associates to the last word.
This is what the percentages mean, 'B' is the consonant, and 'A' is the vowel.:
letter vowel letter : BAB
letter vowel vowel : BAA
vowel vowel letter : AAB
vowel vowel vowel : AAA
The next job for me to do is get the best word from the possible associations and only display that.
This is for my chat-bot project.
-
I see rampant copy and paste - are we learning anything yet?