Like Tree3Likes

I have a list of consonants now I want to use the last wordin the list to compare

This is a discussion on I have a list of consonants now I want to use the last wordin the list to compare within the C Programming forums, part of the General Programming Boards category; Step one: The input has to be split into a string word per line and saved to file. Then fgets ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    229

    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    I'm going to come round to your house and put glue under the ctrl-v key to stop you using it!

    Code:
        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);
    Here, try something like this, start thinking, and stop COPY/PASTING.
    Code:
    char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    for ( i = 0 ; dels[i] != '\0' ; i++ ) {
        delete_char(mystring, dels[i], 0);
    }
    You can reduce 100's of lines of code to a handful in a few minutes, if you just stop and think about it.
    rags_to_riches and ArunS like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Thank you for that, Salem.

    Here's the modified version:

    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 dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    	char mystring[STRING] = {0};
    	char * pch = malloc(300);
    	char *list_of_words = malloc(300);
    	int loop = 0;
    	int loop_2 = 0;
    	int i = 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);
    
    	for ( i = 0 ; dels[i] != '\0' ; i++ ) {
    		delete_char(mystring, dels[i], 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;
    	i = 0;
    	free(pch);
    	free(list_of_words);
    	memset(&mystring[0], 0, sizeof(mystring));
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I'm now able to get the consonants for the words and also the consonant for the last word. So the last line in the text file is able to be used for code stuff. 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
    
    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;
    	FILE *intermediate;
    	char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    	char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    	char mystring[STRING] = {0};
    	char * last = malloc(300);
    	char * pch = malloc(300);
    	char *list_of_words = malloc(300);
    	int loop = 0;
    	int loop_2 = 0;
    	int i = 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);
    
    	for ( i = 0 ; dels[i] != '\0' ; i++ ) {
    		delete_char(mystring, dels[i], 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;   
    	}
    
    	intermediate = fopen("intermediate.txt", "w");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(list_of_words, 300, 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]);
    				}
    			}
    		}	
    	}
    	fclose(list);
    	fclose(intermediate);
    
    	intermediate = fopen("intermediate.txt", "r");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(last, 300, intermediate)!=NULL)
    	{
    	}
    	printf("_%s\n", last);
    
    	free(list_of_words);
    	loop = 0;
    	loop_2 = 0;
    	i = 0;
    	free(pch);
    	free(last);
    	memset(&mystring[0], 0, sizeof(mystring));
    
    	return 0;
    }
    And here is the results:

    Code:
    Input a sentence. Press Enter when done.
    I am jeremy.
    _jrm
    Press any key to continue . . .
    Pretty good eh? Now to compare the other words consonants against the last words consonants to find matching consonants.

    I think I will have to open the intermediate file again and while loop through the file using fgets and do some sort of strncmp comparison against each word in the file in the while loop.

    But any help would be nice.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    So I managed to get results from comparing the consonants of the last word to the other words in the sentence, but now I don't know how to print the original string sentences instead of it's consonant only version.

    Here is my newest code and results:

    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;
    	FILE *intermediate;
    	char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    	char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    	char mystring[STRING] = {0};
    	char * last = malloc(300);
    	char * pch = malloc(300);
    	char * letters = malloc(300);
    	char *list_of_words = malloc(300);
    	int loop = 0;
    	int loop_2 = 0;
    	int i = 0;
    	atexit(MyExit); 
    
    	list = fopen("list.txt", "w");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    		return 0;   
    	}
    
    	/* input a sentence, then save to mystring variable, then the delete unwanted chars from the mystring variable. */
    	printf("Input a sentence. Press Enter when done.\n");
    	fgets(mystring, 4096, stdin);
    
    	for ( i = 0 ; dels[i] != '\0' ; i++ ) {
    		delete_char(mystring, dels[i], 0);
    	}
    
    	/* split sentence into a word per line, and save to list.txt */
    	pch = strtok(mystring, " ");
    
    	while(pch != NULL)
    	{
    		fprintf(list, "%s\n", pch);
    
    		pch = strtok (NULL, " ");
    	}
    
    	fclose(list);
    
    	/* find consonants in the list.txt, write results to intermediate.txt */
    	list = fopen("list.txt", "r");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    		return 0;   
    	}
    
    	intermediate = fopen("intermediate.txt", "w");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(list_of_words, 300, 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]);
    				}
    			}
    		}	
    	}
    	fclose(list);
    	fclose(intermediate);
    	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)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(last, 300, intermediate)!=NULL)
    	{
    	}
    
    	fclose(intermediate);
    
    	/* compare the consonants from the last word to the consonants from the rest of the words. Display the results on the screen. */
    	intermediate = fopen("intermediate.txt", "r");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(letters, 300, intermediate)!=NULL)
    	{
    		printf("\n");
    		if(!(strstr(letters, last)))
    		{
    			if(strpbrk(letters, last))
    			{
    				printf("%s\n", letters);
    			}
    		}
    	}
    	fclose(intermediate);
    
    	free(list_of_words);
    	loop = 0;
    	loop_2 = 0;
    	i = 0;
    	free(pch);
    	free(last);
    	free(letters);
    	memset(&mystring[0], 0, sizeof(mystring));
    
    	return 0;
    }
    And the results:

    Code:
    Input a sentence. Press Enter when done.
    We will make our own energy instead of relying on monopolizing power companies.
    
    
    
    
    
    mk
    
    
    
    wn
    
    
    nrg
    
    
    nstd
    
    
    
    rlng
    
    
    n
    
    
    mnplzng
    
    
    pwr
    
    
    Press any key to continue . . .

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I input a sentence then get the consonants in the last word.
    Then I compare the rest of the sentences consonants agint the last words consonants looking for a match.
    The matching words in consonant only form are saved to file and displayed on screen.

    I don't know how to display the original forms of the matching words though so it's a paraphrased sentence, but with whole words.
    I tried putting two fgets each in their own while loop so both files are read in on while loop, that didn't work.
    I thought about putting the list into memory but that doesn't make sense to me, how would the words be separated.
    I tried some for loops to no avail.
    So I'm stuck right now and could use some sage advice to help me solve this problem.

    Here is the code I have right now.


    This is for my chat-bot project btw in case you were wondering.


    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;
    	FILE *intermediate;
    	FILE *comparison;
    	char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    	char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    	char mystring[STRING] = {0};
    	char * last = malloc(300);
    	char * pch = malloc(300);
    	char * letters = malloc(300);
    	char *list_of_words = malloc(300);
    	int loop = 0;
    	int loop_2 = 0;
    	int i = 0;
    	atexit(MyExit); 
    
    	list = fopen("list.txt", "w");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    		return 0;   
    	}
    
    	comparison = fopen("comparison.txt", "w");
    	if(!comparison)   
    	{
    		perror("Error: file comparison.txt was not found or opened");   
    		return 0;   
    	}
    
    	/* input a sentence, then save to mystring variable, then the delete unwanted chars from the mystring variable. */
    	printf("Input a sentence. Press Enter when done.\n");
    	fgets(mystring, 4096, stdin);
    
    	for ( i = 0 ; dels[i] != '\0' ; i++ ) {
    		delete_char(mystring, dels[i], 0);
    	}
    
    	/* split sentence into a word per line, and save to list.txt */
    	pch = strtok(mystring, " ");
    
    	while(pch != NULL)
    	{
    		fprintf(list, "%s\n", pch);
    
    		pch = strtok (NULL, " ");
    	}
    
    	fclose(list);
    
    	/* find consonants in the list.txt, write results to intermediate.txt */
    	list = fopen("list.txt", "r");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    		return 0;   
    	}
    
    	intermediate = fopen("intermediate.txt", "w");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(list_of_words, 300, 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]);
    				}
    			}
    		}	
    	}
    	fclose(list);
    	fclose(intermediate);
    	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)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(last, 300, intermediate)!=NULL)
    	{
    	}
    
    	fclose(intermediate);
    
    	/* compare the consonants from the last word to the consonants from the rest of the words. Display the results on the screen. */
    	intermediate = fopen("intermediate.txt", "r");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(letters, 300, intermediate)!=NULL)
    	{
    		printf("\n");
    		fprintf(comparison, "\n");
    		if(!(strstr(letters, last)))
    		{
    			if(strpbrk(letters, last))
    			{
    				printf("%s", letters);
    				fprintf(comparison, "%s", letters);
    			}
    		}
    	}
    	fclose(intermediate);
    	fclose(comparison);
    
    	free(list_of_words);
    	loop = 0;
    	loop_2 = 0;
    	i = 0;
    	free(pch);
    	free(last);
    	free(letters);
    	memset(&mystring[0], 0, sizeof(mystring));
    
    	return 0;
    }
    Google has nothing for this problem so if you know how to solve my problem feel free to speak up and be heard!
    I would really appreciate some help with this.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your description is tough to follow exactly. Can you post an example of the input you have, and the output you want? Less description, and more example, please.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    I'm going to come round to your house and put glue under the ctrl-v key to stop you using it!
    Which key? CTRL or V?

    Oh, I know. It's one of those Microsoft Office Keyboards that have a Paste key!


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I have made the code work, now i would like advice on fixing bugs and faulty logic.

    Here is the code:

    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;
    	FILE *intermediate;
    	FILE *comparison;
    	FILE *consonant_words;
    	char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    	char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    	char mystring[STRING] = {0};
    	char * last = malloc(4096);
    	char * pch = malloc(4096);
    	char * letters = malloc(4096);
    	char *list_of_words = malloc(4096);
    	char *whole_consonant_words = malloc(4096);
    	int loop = 0;
    	int loop_2 = 0;
    	int i = 0;
    	int count = 0;
    	atexit(MyExit); 
    
    	list = fopen("list.txt", "w");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    		return 0;   
    	}
    
    	consonant_words = fopen("consonant_words.txt", "w");
    	if(!consonant_words)   
    	{
    		perror("Error: file consonant_words.txt was not found or opened");   
    		return 0;   
    	}
    
    	comparison = fopen("comparison.txt", "w");
    	if(!comparison)   
    	{
    		perror("Error: file comparison.txt was not found or opened");   
    		return 0;   
    	}
    
    	/* input a sentence, then save to mystring variable, then the delete unwanted chars from the mystring variable. */
    	printf("Input a sentence. Press Enter when done.\n");
    	fgets(mystring, 4096, stdin);
    
    	for ( i = 0 ; dels[i] != '\0' ; i++ ) {
    		delete_char(mystring, dels[i], 0);
    	}
    
    	/* split sentence into a word per line, and save to list.txt */
    	pch = strtok(mystring, " ");
    
    	while(pch != NULL)
    	{
    		fprintf(list, "%s\n", pch);
    
    		pch = strtok (NULL, " ");
    	}
    
    	fclose(list);
    
    	/* find consonants in the list.txt, write results to intermediate.txt */
    	list = fopen("list.txt", "r");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    		return 0;   
    	}
    
    	intermediate = fopen("intermediate.txt", "w");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	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;
    		}
    	}
    	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)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	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)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return 0;   
    	}
    
    	consonant_words = fopen("consonant_words.txt", "r");
    	if(!consonant_words)   
    	{
    		perror("Error: file consonant_words.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(letters, 4096, intermediate)!=NULL)
    	{
    		if(strpbrk(letters, consonant))
    		{
    			fgets(whole_consonant_words, 4096, consonant_words);
    			if(strpbrk(letters, last))
    			{
    				printf("%s", whole_consonant_words);
    				fprintf(comparison, "%s", whole_consonant_words);
    			}
    		}
    	}
    	fclose(intermediate);
    	fclose(comparison);
    	fclose(consonant_words);
    	free(whole_consonant_words);
    	free(list_of_words);
    	loop = 0;
    	loop_2 = 0;
    	i = 0;
    	free(pch);
    	free(last);
    	free(letters);
    	memset(&mystring[0], 0, sizeof(mystring));
    
    	return 0;
    }
    Here are two examples of the program running:

    Code:
    Input a sentence. Press Enter when done.
    Scientists are trying to figure out how to link the devices you own directly to
    your brain without an actual physical The Matrix-like cranial plug
    trying
    figure
    link
    directly
    actual
    physical
    Matrixlike
    cranial
    plug
    Press any key to continue . . .
    Code:
    Input a sentence. Press Enter when done.
    We will make our own energy instead of relying on monopolizing power companies
    make
    own
    energy
    instead
    relying
    on
    monopolizing
    power
    companies
    Press any key to continue . . .

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I made my code into a function. I will post the code and the result below:

    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 redefined_sentence ()
    {
    	FILE *list;
    	FILE *intermediate;
    	FILE *comparison;
    	FILE *consonant_words;
    	char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    	char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    	char mystring[STRING] = {0};
    	char * last = malloc(4096);
    	char * pch = malloc(4096);
    	char * letters = malloc(4096);
    	char *list_of_words = malloc(4096);
    	char *whole_consonant_words = malloc(4096);
    	int loop = 0;
    	int loop_2 = 0;
    	int i = 0;
    	int count = 0;
    
    	list = fopen("list.txt", "w");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    		return;   
    	}
    
    	consonant_words = fopen("consonant_words.txt", "w");
    	if(!consonant_words)   
    	{
    		perror("Error: file consonant_words.txt was not found or opened");   
    		return;   
    	}
    
    	comparison = fopen("comparison.txt", "w");
    	if(!comparison)   
    	{
    		perror("Error: file comparison.txt was not found or opened");   
    		return;   
    	}
    
    	/* input a sentence, then save to mystring variable, then the delete unwanted chars from the mystring variable. */
    	printf("Input a sentence. Press Enter when done.\n");
    	fgets(mystring, 4096, stdin);
    
    	for ( i = 0 ; dels[i] != '\0' ; i++ ) {
    		delete_char(mystring, dels[i], 0);
    	}
    
    	/* split sentence into a word per line, and save to list.txt */
    	pch = strtok(mystring, " ");
    
    	while(pch != NULL)
    	{
    		fprintf(list, "%s\n", pch);
    
    		pch = strtok (NULL, " ");
    	}
    
    	fclose(list);
    
    	/* find consonants in the list.txt, write results to intermediate.txt */
    	list = fopen("list.txt", "r");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    		return;   
    	}
    
    	intermediate = fopen("intermediate.txt", "w");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return;   
    	}
    
    	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;
    		}
    	}
    	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)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return;   
    	}
    
    	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)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    		return;   
    	}
    
    	consonant_words = fopen("consonant_words.txt", "r");
    	if(!consonant_words)   
    	{
    		perror("Error: file consonant_words.txt was not found or opened");   
    		return;   
    	}
    
    	while(fgets(letters, 4096, intermediate)!=NULL)
    	{
    		if(strpbrk(letters, consonant))
    		{
    			fgets(whole_consonant_words, 4096, consonant_words);
    			if(strpbrk(letters, last))
    			{
    				fprintf(comparison, "%s", whole_consonant_words);
    			}
    		}
    	}
    	fclose(intermediate);
    	fclose(comparison);
    	fclose(consonant_words);
    	free(whole_consonant_words);
    	free(list_of_words);
    	loop = 0;
    	loop_2 = 0;
    	i = 0;
    	free(pch);
    	free(last);
    	free(letters);
    	memset(&mystring[0], 0, sizeof(mystring));
    
    	return;
    }
    
    void MyExit(void) { system("pause"); }
    
    int main ()
    {
    	FILE *comparison;
    	char * words = malloc(4096);
    	char * one_word = malloc(4096);
    	atexit(MyExit); 
    	redefined_sentence();
    
    	comparison = fopen("comparison.txt", "r");
    	if(!comparison)   
    	{
    		perror("Error: file comparison.txt was not found or opened");   
    		return;   
    	}
    
    	while(fgets(words, 4096, comparison)!=NULL)
    	{
    		printf("%s", words);
    	}
    	fclose(comparison);
    	free(words);
    
    	comparison = fopen("comparison.txt", "r");
    	if(!comparison)   
    	{
    		perror("Error: file comparison.txt was not found or opened");   
    		return;   
    	}
    
    	while(fgets(one_word, 4096, comparison)!=NULL)
    	{
    	}
    	fclose(comparison);
    	printf("\nThe last word is: %s", one_word);
    	free(one_word);
    
    	return 0;
    }
    The result:

    Code:
    Input a sentence. Press Enter when done.
    Strangely, people also use search engines to find other search engines
    Strangely
    also
    use
    search
    engines
    find
    search
    engines
    
    The last word is: engines
    Press any key to continue . . .
    Still waiting to see if anybody has any advice to give me about this code.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I fixed some problems with free and file returns. Here's the newest version:

    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 redefined_sentence ()
    {
    	FILE *list;
    	FILE *intermediate;
    	FILE *comparison;
    	FILE *consonant_words;
    	char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    	char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    	char mystring[STRING] = {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);
    	int loop = 0;
    	int loop_2 = 0;
    	int i = 0;
    	int count = 0;
    
    	list = fopen("list.txt", "w");
    	if(!list)   
    	{
    		perror("Error: file list.txt was not found or opened");   
    	}
    
    	consonant_words = fopen("consonant_words.txt", "w");
    	if(!consonant_words)   
    	{
    		perror("Error: file consonant_words.txt was not found or opened");    
    	}
    
    	comparison = fopen("comparison.txt", "w");
    	if(!comparison)   
    	{
    		perror("Error: file comparison.txt was not found or opened");     
    	}
    
    	/* input a sentence, then save to mystring variable, then the delete unwanted chars from the mystring variable. */
    	printf("Input a sentence. Press Enter when done.\n");
    	fgets(mystring, 4096, stdin);
    
    	for ( i = 0 ; dels[i] != '\0' ; i++ ) {
    		delete_char(mystring, dels[i], 0);
    	}
    
    	/* split sentence into a word per line, and save to list.txt */
    	pch = strtok(mystring, " ");
    
    	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)   
    	{
    		perror("Error: file list.txt was not found or opened");     
    	}
    
    	intermediate = fopen("intermediate.txt", "w");
    	if(!intermediate)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");    
    	}
    
    	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)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");   
    	}
    
    	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)   
    	{
    		perror("Error: file intermediate.txt was not found or opened");     
    	}
    
    	consonant_words = fopen("consonant_words.txt", "r");
    	if(!consonant_words)   
    	{
    		perror("Error: file consonant_words.txt was not found or opened");   
    	}
    
    	while(fgets(letters, 4096, intermediate)!=NULL)
    	{
    		if(strpbrk(letters, consonant))
    		{
    			fgets(whole_consonant_words, 4096, consonant_words);
    			if(strpbrk(letters, last))
    			{
    				fprintf(comparison, "%s", whole_consonant_words);
    			}
    		}
    	}
    
    	free(last);
    	free(whole_consonant_words);
    	free(letters);
    
    	fclose(intermediate);
    	fclose(comparison);
    	fclose(consonant_words);
    	loop = 0;
    	loop_2 = 0;
    	i = 0;
    	memset(&mystring[0], 0, sizeof(mystring));
    }
    
    void MyExit(void) { system("pause"); }
    
    int main ()
    {
    	FILE *comparison;
    	char * words = malloc(4096);
    	char * one_word = malloc(4096);
    	atexit(MyExit); 
    	redefined_sentence();
    
    	comparison = fopen("comparison.txt", "r");
    	if(!comparison)   
    	{
    		perror("Error: file comparison.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(words, 4096, comparison)!=NULL)
    	{
    		printf("%s", words);
    	}
    	fclose(comparison);
    	free(words);
    
    	comparison = fopen("comparison.txt", "r");
    	if(!comparison)   
    	{
    		perror("Error: file comparison.txt was not found or opened");   
    		return 0;   
    	}
    
    	while(fgets(one_word, 4096, comparison)!=NULL)
    	{
    	}
    	fclose(comparison);
    	printf("\nThe last word is: %s", one_word);
    	free(one_word);
    
    	return 0;
    }
    How does it look so far?

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Here is my newest version of my code. How does it look?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define STRING 4096
    #define MAX_STRING 4096
    
    /* 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)
    				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');
    	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;
    }
    
    void redefined_sentence (char* sentence)
    {
    	FILE *list = NULL;
    	FILE *intermediate = NULL;
    	FILE *comparison = NULL;
    	FILE *consonant_words = NULL;
    	char consonant[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
    	char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
    	char mystring[STRING] = {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 * words = malloc(4096);
    	char * one_word = 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;
    	}
    
    	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(comparison, "%s", whole_consonant_words);
    			}
    		}
    	}
    
    	free(last);
    	free(whole_consonant_words);
    	free(letters);
    	fclose(comparison);
    
    	//////////////
    
    	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);
    
    	comparison = fopen("comparison.txt", "r");
    	if(comparison == NULL) 
    	{
    		perror("Error: file comparison.txt was not found or opened");   
    		goto Cleanup;
    	}
    
    	while(fgets(one_word, 4096, comparison)!=NULL)
    	{
    	}
    	fclose(comparison);
    	printf("\nThe last word is: %s\n", one_word);
    	free(one_word);
    
    	//////////////
    
    	fclose(intermediate);
    	fclose(consonant_words);
    
    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;
    	}
    
    	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)
    	{
    		printf("", read_book);
    	}
    	free(read_book);
    	fclose(book);
    
    	return 0;
    }
    And the example of the program running:

    Code:
    Input a sentence. Press Enter when done.
    The scientists now believe that they have found a path that could enable more po
    werful microscopes, telecommunications and computers. Specifically, the discover
    y is expected to have effects on technologies for "steering and shaping laser be
    ams for military and communications applications, nanocircuits for computers tha
    t use light to process information, and new types of powerful lenses for microsc
    opes."
    
    scientists
    that
    they
    path
    that
    could
    more
    powerful
    microscopes
    telecommunications
    computers
    
    The last word is: computers
    
    Specifically
    discovery
    is
    expected
    effects
    technologies
    for
    steering
    shaping
    laser
    beams
    for
    military
    communications
    applications
    nanocircuits
    for
    computers
    use
    process
    information
    types
    powerful
    lenses
    for
    microscopes
    
    The last word is: microscopes
    
    Press any key to continue . . .

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    I would suggest that you need more functions so you can get rid of the goto statements. You should try to get each function to do one thing if possible. You might also want to open and test your file opening before all of your mallocs. That way you can just return an error code to the calling function and not have to worry about all the cleanup of the variables.

    Jim

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You have a lot of code that could be optimized... for example:

    Code:
    
    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;			}
    should be:

    Code:
    if ( (file_character == EOF) || (file_character == '.') || (file_character == '?') || (file_character == '!') ) {
          buffer[character] = 0;
          break;
    }

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I have updated the code, now I tried to get the files open before declaring the other variables but I got some errors, and I included that || code, thanks.

    Here's the new 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
    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(&copy_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:

    Code:
    Input a sentence. Press Enter when done.
    an kind no entry.
    
    an kind no entry
    The last word is: entry
    
    letter vowel letter 0.250000
    letter vowel vowel 0.250000
    vowel vowel letter 0.250000
    vowel vowel vowel 0.250000
    
    Press any key to continue . . .
    And here it is in actual usage:

    Code:
    Input a sentence. Press Enter when done.
    The Galaxy Note, Samsung's unconventional powerhouse, has managed to ship 1 mill
    ion units in under two months.
    
    The Note Samsungs unconventional powerhouse has managed to ship million units in
     under two months
    The last word is: months
    
    letter vowel letter 0.400000
    letter vowel vowel 0.333333
    vowel vowel letter 0.266667
    vowel vowel vowel 0.000000
    
    Press any key to continue . . .

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  2. Replies: 16
    Last Post: 08-25-2008, 11:08 PM
  3. Left justification using linked list of link list
    By hykyit in forum C Programming
    Replies: 7
    Last Post: 08-29-2005, 10:04 PM
  4. Nested for loop...search & display a list within a list
    By chadsxe in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2005, 01:34 PM
  5. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21