I have a list of consonants now I want to use the last wordin the list to compare
Step one:
The input has to be split into a string word per line and saved to file.
Then fgets reads each line and stops when the file is NULL.
Step two:
Then I want to For loop the file and if a word in the list has a consonant I want to print that in the console window.
Step three:
Then using the lists last word consonants, starting at the first word in the list, I want to compare the other words consonants to it.
When the consonants match, the next word in the list is compared,
When the next word doesn't share a consonant with the last word the comparison ends.
Then I want to print the matching words and last word in the console window.
Here is my code so far:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define STRING 4096
#define MAX_STRING 4096
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;
}
void MyExit(void) { system("pause"); }
int main ()
{
FILE *list;
char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
char mystring[STRING] = {0};
char * pch = malloc(300);
char *list_of_words = malloc(300);
int loop = 0;
int loop_2 = 0;
atexit(MyExit);
list = fopen("list.txt", "w");
if(!list)
{
perror("Error: file list.txt was not found or opened");
return 0;
}
printf("Input a sentence. Press Enter when done.\n");
fgets(mystring, 4096, stdin);
delete_char(mystring, '.', 0);
delete_char(mystring, '\n', 0);
delete_char(mystring, '`', 0);
delete_char(mystring, '1', 0);
delete_char(mystring, '2', 0);
delete_char(mystring, '3', 0);
delete_char(mystring, '4', 0);
delete_char(mystring, '5', 0);
delete_char(mystring, '6', 0);
delete_char(mystring, '7', 0);
delete_char(mystring, '8', 0);
delete_char(mystring, '9', 0);
delete_char(mystring, '0', 0);
delete_char(mystring, '-', 0);
delete_char(mystring, '=', 0);
delete_char(mystring, '~', 0);
delete_char(mystring, '!', 0);
delete_char(mystring, '@', 0);
delete_char(mystring, '#', 0);
delete_char(mystring, '$', 0);
delete_char(mystring, '%', 0);
delete_char(mystring, '^', 0);
delete_char(mystring, '&', 0);
delete_char(mystring, '*', 0);
delete_char(mystring, '(', 0);
delete_char(mystring, ')', 0);
delete_char(mystring, '_', 0);
delete_char(mystring, '+', 0);
delete_char(mystring, '[', 0);
delete_char(mystring, ']', 0);
delete_char(mystring, '\\', 0);
delete_char(mystring, '{', 0);
delete_char(mystring, '}', 0);
delete_char(mystring, '|', 0);
delete_char(mystring, ';', 0);
delete_char(mystring, '\'', 0);
delete_char(mystring, ':', 0);
delete_char(mystring, '"', 0);
delete_char(mystring, ',', 0);
delete_char(mystring, '/', 0);
delete_char(mystring, '<', 0);
delete_char(mystring, '>', 0);
delete_char(mystring, '?', 0);
pch = strtok(mystring, " ");
while(pch != NULL)
{
fprintf(list, "%s\n", pch);
pch = strtok (NULL, " ");
}
fclose(list);
list = fopen("list.txt", "r");
if(!list)
{
perror("Error: file list.txt was not found or opened");
return 0;
}
while(fgets(list_of_words, 300, list)!=NULL)
{
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])
{
printf("%c", consonant[loop_2]);
}
}
}
puts("\n");
}
fclose(list);
loop = 0;
loop_2 = 0;
free(pch);
free(list_of_words);
memset(&mystring[0], 0, sizeof(mystring));
return 0;
}
This is what the output looks like:
Code:
Input a sentence. Press Enter when done.
hello world I am here
hll
wrld
m
hr
Press any key to continue . . .
I have step one and two from list of things to do but for the third step I can use some guidance please. :confused: