1

I have this code

    char output[1000] = "";
    int numWords = 0;
    int wordsRemoved = 0;
    char word[50]; // word that needs to be removed
    char ch; //character directly following the word being checked (always space or newline)
    char check[50]; // word that is being checked

    scanf("%s", word);


    while(!feof(stdin)){
        scanf("%s%c", check, &ch);
        numWords++; //counting total words

        if(strcmp(check, word) == 0){
            wordsRemoved++; //counting words removed
        }

        if(strcmp(word, check) != 0){
            strcat(output, check);   //checks if word should be removed and if not appends to str
            strncat(output, &ch, 1);
        }else if(strcmp(word, check) == 0 && ch == '\n'){
            strncat(output, &ch, 1);  // supposed to print a newline if the delete word was
                                      // on the end of a string but it gives me too many
                                      // newlines, and not enough if I remove it
        }
    }
    printf("%s", output); //prints new string

    return 0;

which is supposed to take an input text file and read until end of file, then remove a word specified by the user and then print the new string back out

For example, the test text file I am using reads:

cs
cs hello there
hello there cs
cs
hello there cscs there cs.
cs cs cs
test

Then the output should be

hello there
hello there
hello cscs there cs.
test

but I am instead getting

hello there
hello there

hello cscs there cs.

test

As you can see, I'm very close to figuring this out, but any line that consists of just the delete word ends up with an extra unwanted newline that I cannot figure out how to circumvent

Any idea how to get around this?

Also I am still learning to code so please don't use anything crazy advanced :)

Thanks for your help

Ryan
  • 11
  • 1
  • 1
    if you want to remove empty lines, you should keep track of whether you have already printed anything on the current line. If you see a newline, print it only if your word counter is positive, then reset the counter to zero. – M Oehm Feb 24 '22 at 17:58
  • 1
    Not related to your empty lines but you might want to take a closer look at [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Gerhardh Feb 24 '22 at 18:06
  • @MOehm I tried using your advice to fix my problem but I'm not entirely sure if it is doing anything. I tried adding a condition to the if statement to only concatenate the newline if numWords was greater than zero (if(ch == '\n' && strcmp(word, check) == 0 && numWords > 0)), but the output didn't change, did I misunderstand your advice or doing something wrong? – Ryan Feb 24 '22 at 19:17
  • [Here](https://ideone.com/yw91cz) is some code that does what you want. Printing a word and printing the space or newline are treated separately: If you must decide whether to print a newline or not, check whether you have written any words on the current line; if you must decide whether to print a space or not, check whether you have written the last word. – M Oehm Feb 25 '22 at 06:53
  • (That code uses `fscan(f, ...`)` to read from a file in memory, so that everything is contained in the example, you can replace that with `scanf`'s. It also prints instead of concatenating, because `strcat`ting to a fixed-size buffer from potentially infinite input is a recipe for disaster. The word to skip is hardcoded to make the example easier.) – M Oehm Feb 25 '22 at 06:53

0 Answers0