473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Replace words in string

Hello,

I'm looking to write a program that will take a user input (a sentence) and make it less sexist.
For ex., User input is -> He who laughs last, laughs best.
Program output -> She or he who laughs last, laughs best.
or
"A student can get a discount if he shows his ID."

should be replaced by

"A student can get a discount if he or she shows his or her ID."

I'm a little lost on how to scan the string to pick up the pronouns and replace them. Below is what I have so far. I don't expect someone to write this for me, but am looking for a push in the right direction. Any info, source code examples, links, or hints would be most appreciated.

Thanks in advance.
WR




Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.     string strInput;
  9.  
  10.     cout << "\nPlease enter a gender specific sentence\n";
  11.  
  12.     cout << "or enter `I'm done' to exit the program.\n";
  13.  
  14.          getline(cin, strInput);  //gets the user input
  15.  
  16.           do
  17.               {
  18.             string::size_type loc = strInput.find("he", "him", 0);
  19.  
  20.             if( loc = string::npos )
  21.             string strInput.replace("he", "he or she")||
  22.             string strInput.replace("him", "him or her")
  23.             cout << strInput;
  24.  
  25.             else 
  26.             cout << strInput;
  27.  
  28.             }
  29.             while 
  30.                 (strInput != "I'm Done");
  31.  
  32.     return 0;
  33.     }
  34.  
  35.  
  36.  
  37. }
Dec 10 '06 #1
7 8830
nickyeng
254 100+
find() in string takes one string and one index as following:

size_type find( const string& str, size_type index );

are you sure there is a function for 2 string in find() ?
Dec 10 '06 #2
You mean where I have two instances where I'm trying to look for he and then scanning for him?

No, I'm not sure if it's possible to do it like that.
Dec 10 '06 #3
nickyeng
254 100+
there are many errors in your program.

firstly, find() function for string is as following :

Expand|Select|Wrap|Line Numbers
  1. size_type find( const string& str, size_type index );
  2.   size_type find( const char* str, size_type index );
  3.   size_type find( const char* str, size_type index, size_type length );
  4.   size_type find( char ch, size_type index );
  5.  
and your replace() function is also an error argument, it should be one of the following:

Expand|Select|Wrap|Line Numbers
  1.  string& replace( size_type index, size_type num, const string& str );
  2.   string& replace( size_type index1, size_type num1, const string& str, size_type index2, size_type num2 );
  3.   string& replace( size_type index, size_type num, const char* str );
  4.   string& replace( size_type index, size_type num1, const char* str, size_type num2 );
  5.   string& replace( size_type index, size_type num1, size_type num2, char ch );
  6.   string& replace( iterator start, iterator end, const string& str );
  7.   string& replace( iterator start, iterator end, const char* str );
  8.   string& replace( iterator start, iterator end, const char* str, size_type num );
  9.   string& replace( iterator start, iterator end, size_type num, char ch );
  10.  
You can't take 2 string argument in replace() and find().
Dec 10 '06 #4
So, in the find part of the code where you you have const string& str, str should be the name of my string (the user input) correct?

Also on the 2nd part with the replace I'd need to use one that iterates because it needs to continue to loop until the user tells it to stop, right? Or does this mean it goes through the string and iterates until all replacements or done?
Dec 10 '06 #5
We also weren't assigned a book for the class, so having to go to outside resources to find examples of everything is quite difficult.
Dec 10 '06 #6
nickyeng
254 100+
We also weren't assigned a book for the class, so having to go to outside resources to find examples of everything is quite difficult.
mine more worst,
i'm self-study.
I only get the material(useless i can tell) from university and study at home alone.
That's why i am here to get help from seniors.

For your questions, i no time to compile and to fix it because i dont have that skills to solve ppl's problem yet.
hehe.
Sorry for that.

be patient to wait, because seniors are helpful.
good luck.
Dec 10 '06 #7
Thanks for the effort; I truly do appreciate any time you take to help.
Dec 10 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: susan0nz | last post by:
I have some strings like this: "The temperature is 18 Celsius today". Some strings may have more than one value in them, like "The temperature is 18 Celsius today. Yesterday is was 17 Celsius". ...
2
by: Babu Mannaravalappil | last post by:
Hi, I want to replace some words in my text files (actually transpose). For example, I have a whole lot of expressions (words) in my files as follows: TABLECUSTOMERS TABLEORDERS...
4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
24
by: Wim Roffal | last post by:
Is there a possibility to do a string replace in javascript without regular experessions. It feels like using a hammer to crash an egg. Wim
4
by: Prasad S | last post by:
Hello I wish to replace all the characters in a string except those which are inside '<' & '>' characters. And there could be multiple occurences of < & > within the string. e.g. string =...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
10
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal...
7
stealwings
by: stealwings | last post by:
I have a little problem with my program, or maybe it is not that little, anyway here is the code: #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using...
12
pntkiran
by: pntkiran | last post by:
HI All. I want to write a code which replace words from string.without use of string function Example str = "This is string" remove "is" from string and replace with "was". so, finally str =...
8
by: Avi | last post by:
Hi all, I'm using string Replace(string oldValue, string newValue) and would like it to replace only full words that matches oldValue and not when oldValue is a substring of a larger word. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.