473,385 Members | 1,838 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.

how to do a replace string?

Hi,

lets say if i have a string "mississippi". is there any method available in c++ that let me say replace all the "s" into maybe "a"? and the result of the query would be "miaaiaaippi".

Hope u all understand my above query and would gladly appreciate if anyone could help!
Oct 15 '07 #1
6 1686
oler1s
671 Expert 512MB
You could iterate over the string, checking if the character is 's' and make it 'a' instead. I don't know of any built in C++ string method that will do this character find and replace.
Oct 15 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
This is where you use the transform algorithm:
Expand|Select|Wrap|Line Numbers
  1. transform(str.begin(), str.end(), convertedstr.begin(), aFunction);
  2.  
transform will call the unary function aFunction with each element of str. This function can test for s and change it to a.

Algorithms are put into the STL to avoid writing billions of member functions on classes like string.
Oct 15 '07 #3
For the transform method, is there any website i can refer to get examples and more on the arguments. i cant seem it find it on the net.
i am wondering how to input the above method for my case
Oct 16 '07 #4
Laharl
849 Expert 512MB
For the transform method, is there any website i can refer to get examples and more on the arguments. i cant seem it find it on the net.
i am wondering how to input the above method for my case
http://www.cplusplus.com/reference/algorithm/transform.html

The examples are a little farther down from all the complicated stuff.
Oct 16 '07 #5
any idea how i can iterate a string? i am having trouble understanding the transform method. thus decided to go with iterating over the whole string and changing instead. any suggestions?
Oct 17 '07 #6
sicarie
4,677 Expert Mod 4TB
Did you read through the tutorial above? What part were you confused on? We can help you figure that out.

But if you're set on iterating through the string, just create a pointer to a char and point it at the location where the string begins.
Oct 17 '07 #7

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

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
9
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
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...
5
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.