1

I'm looking to replace words from a file where words.txt contains "banana test apple" would output -> "banana Replaced! apple" The words it would be looking to replace would come as an argument to stdin ("test" in this case)

Not sure where my code is going wrong, and I'm also wondering if there's a better way to do it than get getchar() function, as I find it confusing to work with, and annoying as you cannot backup characters.

Any suggestions? Thanks

    $ ./a.exe test < words.txt


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

    int main(int argc, char** argv) { 
        int c = getchar();    
        int counter = 0;


        int i,index;
        for (i = 1; i < argc; i++){
            for (index = 0; index < strlen(argv[i]); index++){
                while (c == argv[i][index]){
                    counter++;
                    c = getchar();
                    index++;
                    if (counter == strlen(argv[i])){
                        printf("Replaced!");
                    }
                }
                printf("%c ",c);
                c = getchar();
                counter = 0;
            }
        }
        return (0);
    }
  • An obvious suggestion is to read whole lines with `fgets()` and then search those (maybe with `strstr()`) to find the word to be replaced, and then print the material before the word and the replacement text before resuming the search from after the matched word in the line (so a line containing `"testing, 1, 2, 3, tested!"` ends up as `"Replaced!ing, 1, 2, 3, Replaced!ed!"`. – Jonathan Leffler Oct 03 '14 at 00:41
  • 1
    Can you explain the algorithm you think your code implements -- or at least add comments to it? – David Schwartz Oct 03 '14 at 00:44
  • Please supply any input to your program and any output it produces when you ask a question. So don't just show us the command line you're running, but show us the input and output. As it stands, with this command line and code you're piping the contents of words.txt to stdin *(so its the source of data for your getchar() calls)* and then you're looping over the letters of argv[1] "test" for some reason to get t-e-s-t. It in no way lines up with your description. As @DavidSchwartz says, at least comment your misconceptions so we have a chance at addressing them. – HostileFork says dont trust SE Oct 03 '14 at 00:53
  • @HostileFork: The input to the program comes from stdin, but the source of words to replace comes from the command line. – dreamlax Oct 03 '14 at 01:03
  • @dreamlax How do you figure that from `./a.exe test < words.txt` ? :-/ The program doesn't open any files itself, uses contents of words.txt to pipe stdin, and for some reason looks at the characters t-e-s-t from argv[1]. – HostileFork says dont trust SE Oct 03 '14 at 01:26
  • @HostileFork: He explains it in the first paragraph. The only bit that doesn't make sense is "as an argument to stdin" but I just assumed he meant "as arguments". – dreamlax Oct 03 '14 at 03:52
  • @dreamlax My question isn't about what he wants to accomplish. It's about the complete lack of correspondence between what is written and shown as the supposed pursuit of that goal. If you can explain how the code and invocation shown *lines up in any way to the problem statement*, then I will be surprised. – HostileFork says dont trust SE Oct 03 '14 at 04:06
  • @HostileFork: The code doesn't line up, which is why [I assume] he's asking the question. OP is obviously having difficulty determining a suitable algorithm. As it is, it looks like OP is trying to read input character-by-character and comparing that input character-by-character to the command line argument. When he says he can't "backup" characters I assume he means you can't put them back into the input stream (in the case where the input word doesn't match any of the words provided on the command line). – dreamlax Oct 03 '14 at 04:17

2 Answers2

1

I would do it as follows :

  1. read all the file into a buffer using fgets
  2. looking for the key work (test) using strstr
  3. replace it with your new string using strncpy
  4. write to file (or output to stdout) whatever you want to do
chouaib
  • 2,763
  • 5
  • 20
  • 35
0

You could use scanf to read in a word at a time:

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

int main(int argc, char *argv[])
{
    char wordbuffer[100];
    while (scanf("%99s", wordbuffer) == 1)
    {
        int replace = 0;
        for (int i = 1; i < argc && !replace; i++)
            if (strcmp(wordbuffer, argv[i]) == 0)
                replace = 1;

        if (replace)
            printf("Replaced! ");
        else
            printf("%s ", wordbuffer);
    }
}

If you are using a modern system that compliant with the latest POSIX specification, you can take advantage of the m assignment-allocation character, and have the appropriate amount of space allocated for you, so that you don't have to use some arbitrary maximum number of characters.

int main(int argc, char *argv[])
{
    char *wordbuffer;
    while (scanf("%ms", &wordbuffer) == 1)
    {
        int replace = 0;
        for (int i = 1; i < argc && !replace; i++)
            if (strcmp(wordbuffer, argv[i]) == 0)
                replace = 1;

        if (replace)
            printf("Replaced! ");
        else
            printf("%s ", wordbuffer);

        free(wordbuffer);
    }
}
dreamlax
  • 93,976
  • 29
  • 161
  • 209