Question About Finding a Word and Replacing It
Hello,
I asked a question about this program a few days ago, but now I have a new problem. What I want to do now is search through the line that's read in from stdin, and then replace one word with another (provided from command line arguments). I'm trying to use the strstr() function to check if the word is there, but I can't think of how to replace the word with the other one. Here is what I have:
Code:
#include <stdio.h>
#include <string.h>
main(int argc, char *argv[]) {
char buf[BUFSIZ];
// read lines from stdin
while ( fgets(buf, BUFSIZ, stdin) ) {
if (strstr(buf,argv[1]) == NULL) puts(buf);
else puts(argv[2]);
}
All that does is prints out argv[2] when strstr is not null. Any help would be greatly appreciated.