Friday, 15 August 2014

string - Censor words in c -


I am working on a function that censors the words I have created this function and it works, except That it accepts only one argument. For example, when I do "./a.out test & lt; example.txt", then it is censored in "example.txt" by replacing "test.txt" instead of "test.txt". But when I add another, like "./a.out test test2", it changes only "test", not both "test" and "test2".

Can anyone help me with my function? I do not know what I'm doing.

Here is the function:

  #include & lt; Assert.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; Int main (Int arc, four ** argv) {emphasis (argc> 1); Char fileRead [4096]; Four places [] = "sensor"; Size_t word_len = strlen (argv [1]); Whereas (fgets (fileRead, sizeof (fileRead), stdin) = 0) {char * start = fileRead; Char * word_at; While ((word_at = strstr (start, argv [1])) = 0) {printf ("%. *% S", (int) (word_at - start), start, change); Start = word_at + word_len; } Printf ("% s", start); } Printf ("\ n"); Return (0); }  

I suggest you have a mid loop on argv element 1 to argc - 1 . In each repetition, it will process the line to sensitize the same logic, as if your function is now only for the first argument.

  int arg; (Arg = 1; arg and lt; organi; arg = + 1) {/ * ... sensor argv [arg] ... * /}  

Note that you Then, by dropping by () , logic access is avoided in such a way that naturally avoids reaching out of its range of argv (if No logic is specified, anything will run without program censoring).


No comments:

Post a Comment