Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?

What I have tried:

#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);
    }
Posted
Updated 9-Dec-21 23:00pm
Comments
Richard MacCutchan 10-Dec-21 4:29am    
Don't use fgetc as it only reads one character at a time. Use fgets which reads complete lines. You can then test if the final character is an exclamation mark. If not then use fputs to write it to the new file.

Start by identifying whole sentences: they generally end with one of these characters '.', '?', '!'.
When you have that working, you have two choices, which are very similar to teh two ways you might do it in the real world:

1) Build a new output string as you go, but not copying '! sentences.
2) Build a collection of sentences, and remove all those ending with '!'

The first is not too bad: you need to allocate a new chunk of memory to hold all the text, remember where a sentence started and copy text to the end of the sentence. If it ends with a '!' move the output back to where it started so the next sentence overwrites it. If it doesn't, just remember the end of that sentence as the new "input point"

The second is more complex to implement, but simple in principle: create a collection (of your preference) and copy each sentence to it when you identify them. Then process the collection sentence by sentence, and remove the '!' lines from the collection.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
In such a basic language as C you need to implement it yourself but in higher languages like C++ this task would do some string class.

You need at first analyze and find all sentences (delimeters positions) in the string and than copy only the wanted data into the result string.

Use strtok to find delimiter.

Search for the next of the delimiters, if it isnt an exclamation than copy, else skip that part. Be aware of the nasty fineprint of strtok and check all cases.

tip: write test code and make console output while debugging.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900