Working with files


Joined
Dec 10, 2021
Messages
4
Reaction score
0
Hello help please

there is a text file, I want to remove from it all sentences ending with an exclamation mark.
I only did what removes the mark itself, how do I remove the whole sentence?

C:
#include <stdio.h>
#include <stdlib.h>
 
    int main(void)
    {
        int c = 0;
        const unsigned char discard = '!';
        const char *file_name = "ex1.txt";
        FILE *input_file = NULL, *output_file = NULL;
    
        if (!(input_file = fopen(file_name,"r")) || !(output_file = fopen("ex2.txt","w")))
        {
           fprintf(stderr,"error handling");
            exit(EXIT_FAILURE);
        }
    
        while ((c = fgetc(input_file)) != EOF)
        {
            if (c != discard)
            {
                fputc(c,output_file);
            }
        }
 
        fclose(input_file);
            
        exit(EXIT_SUCCESS);
    }
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
29
You'll need to start keeping track of the start of sentences. Define all punctuation characters. If you find a punctuation and it's not an exclamation mark, output from your beginning-of-sentence location and the current one. If the punctuation is an exclamation mark, just don't output it. In either case, update your beginning-of-sentence to the point after the punctuation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top