0
#include <stdio.h>
#include <string.h>

int main() {
    int counter1, counter2;
    char line[200] = ""; //store all words that don't need to be deleted
    char deleteWord[100]; //word that needs to be deleted
    char space;
    char word[100];
    scanf("%s", deleteWord);

    while (1) {
        scanf("%s", word);

        if (feof(stdin))
            break;
        // increment counter of total words
        ++counter1;
        if (strcmp(word, deleteWord) == 0) {
            // see if the word read in == delete word
            // increment counter of deleted words
            ++counter2;
        } else
        if (strcmp(word, " ") == 0) { // space is an actual space
            strcat(line, word);
            strcat(line, " ");
        } else
        if (strcmp(word, "\n")) { // space a new line \n
            strcat(line, word);
            strcat(line, "\n");
        }
    }

    printf("--NEW TEXT--\n%s", line);

    return 0;
}

In summary, my code is supposed to remove a user input string (one or more words) from another user input string (containing or not containing the word(s)) and produce the output. The code removes the word but it adds a newline per word for each iteration. I believe it is doing this because the expression for the second else if is always true. However, when I properly add the strcmp function for the second else if statement, the code does not produce an output at all (no compiler errors - just missing input). Why is this happening and how do I do a strcmp function for a newline?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 2
    Did you mean to have a `== 0` in the test of your last call to `strcmp()` ? – Jeremy Friesner Feb 26 '22 at 00:15
  • First `scanf()` doesn't read '\n' character into buffer. 2. `scanf()` will get you only one word, then statement & code "my code is supposed to remove a user input string (one or more words)" doesn't match. make up your mind. I.e what if user wants to remove two different words? – जलजनक Feb 26 '22 at 00:17
  • If you want to read a whole line of input consiting of several words, instead of a single word, then you should use [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead of `scanf`. – Andreas Wenzel Feb 26 '22 at 00:41
  • 1
    Instead of checking for `feof( stdin );`, it would probably be more appropriate to check the return value of `scanf`. Otherwise, if the last line before end-of-file does not end with a `'\n'`, the loop will terminate without processing the last line. Related, but does not completely apply here: [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/12149471) – Andreas Wenzel Feb 26 '22 at 01:00

1 Answers1

0

Your read the words with scanf("%s", word), which poses these problems:

  • all white space is ignored, so you cannot test for spaces nor newlines as you try to do in the loop, and you cannot keep track of line breaks.

  • you should tell scanf() the maximum number of bytes to store into the destination array word, otherwise any word longer than 99 characters will cause a buffer overflow and invoke undefined behavior.

  • you should test the return value of scanf() instead of callin feof() which might be true after the last word has been successfully read. You should simply write the loop as

      while (scanf("%99s", word) == 1) {
          // increment counter of total words
          ++counter1;
          ...
    
  • you do not test if the words fit in the line array either, causing a buffer overflow if the words kept amount to more than 199 characters including separators.

To delete a specific word from a stream, you could read one line at a time and delete the matching words from the line:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main() {
    char deleteWord[100]; //word that needs to be deleted
    char line[1000]; //store all words that don't need to be deleted

    printf("Enter the word to remove: ");
    if (scanf("%99s", deleteWord) != 1)
        return 1;

    // read and discard the rest of the input line
    int c;
    while ((c = getchar()) != EOF && c != '\n')
        continue;

    size_t len = strlen(deleteWord);
    printf("Enter the text: ");
    while (fgets(line, sizeof line, stdin)) {
        char *p = line;
        char *q;
        while ((p = strstr(p, deleteWord)) != NULL) {
            if ((p == line || isspace((unsigned char)p[-1]))
            &&  (p[len] == '\0' || isspace((unsigned char)p[len]))) {
                /* remove the word */
                memmove(p, p + len, strlen(p + len) + 1);
            } else {
                p += len;
            }
        }
        /* squeeze sequences of spaces as a single space */
        for (p = q = line + 1; *p; p++) {
            if (*p != ' ' || p[-1] != ' ')
                *q++ = *p;
        }
        *q = '\0';
        fputs(line, stdout);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189