Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** censor input (generic challenge question)
- * replaces all words that are the same length as the replacement string
- * provided as argv[1] (default "****") outputting each line with an
- * intervening newline (blank line).
- */
- #include <iostream>
- #include <sstream>
- #include <string>
- int main (int argc, char **argv) {
- std::string line; /* string to hold each line */
- const std::string repl = argc > 1 ? argv[1] : "****"; /* replacement */
- while (getline (std::cin, line)) { /* read each line */
- std::string word; /* string to hold each word */
- std::stringstream s (line); /* stringstream to parse line */
- while (s >> word) /* read each word */
- if (word.length() == repl.length()) /* lengths equal, replace */
- line.replace (line.find (word, 0), repl.length(), repl);
- std::cout << line << "\n\n"; /* output newlines */
- }
- }
- /*
- **Example Input**
- $ cat ../dat/captnjack.txt
- This is a tale
- Of Captain Jack Sparrow
- A Pirate So Brave
- On the Seven Seas.
- **Example Use/Output**
- $ ./getline_repl < ../dat/captnjack.txt
- **** is a ****
- Of Captain **** Sparrow
- A Pirate So Brave
- On the Seven Seas.
- Specifying the replacement as the 1st program argument
- $ ./getline_repl +++++ < ../dat/captnjack.txt
- This is a tale
- Of Captain Jack Sparrow
- A Pirate So +++++
- On the +++++ +++++
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement