I'm looking to wrtie a C++ program that takes a user input and makes it less sexist. For ex., if user input is " I like the way he does it."
The program will scan the input for the pronoun "he" and replace it with he or she. It needs to do the same thing with him, replacing to him or her. I can handle the loop and basic things. Just need some info to get started please. Any replies, links, or info would be most appreciated.
Thanks in advance.
Help with C++ and replacing strings?
You're going to need more than simple string manipulation, in the end. However, you can start there and discover the fabulous details of the english language. Eventually, you're going to discover that a pronoun, if referring to a proper noun already identified, *shouldn't* be changed. Example: "John wanted a drink. He or she went to the fridge."
Anyway, sticking to simple strings, you don't even need C++. Shell scripting using awk or sed will give you this effect in about 5 lines. I would research using sed or awk (or equivalent) under Linux/Unix/Mac or under Cygwin on Windows.
If you're doing this as an academic exercise in C++, then here goes:
Read a block of characters into a buffer ( 1024 characters ) from a file using "gets" or "getline". To account for word split across your read boundaries, parse only up to the last space (not the end of the buffer). Then discard all but what's left of your buffer, shift the remain to the top of the buffer, and load more from the file. Okay so that's block file reading instead of line-based.
In the parsing stage, you have to determine if you're going to account for quoted text. Are quotes around a block of text going to also be changed? If so, then you don't care about anything except your keywords and their replacements.
Use the STL template class "string" with the "replace" function, which can be used with "find" variant to search/replace the strings:
- "he" -%26gt; "he or she"
- "He" -%26gt; "He or she"
- "she" -%26gt; "he or she"
- "She" -%26gt; "He or she"
- "his" -%26gt; "his or her"
- "His" -%26gt; "His or her"
- "her" -%26gt; "his or her"
- "Her" -%26gt; "His or her"
- "him" -%26gt;"him or her"
- "Him" -%26gt; "Him or her"
You know the result is going to be almost completely unreadable, right? A more interesting academic problem may be to completly switch the genders of the pronouns entirely. But, 'tis your program to write, not mine.
bottle palm
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment