473,387 Members | 1,578 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,387 software developers and data experts.

replace occurences in a string

hey,

i have to use the commands find and replace to replace all the occurrences of sydney to melbourne.

I have did this, but not sure if im on the right track:

Expand|Select|Wrap|Line Numbers
  1. #include <string.h>
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. string s1="sydney";
  11. cout << s1.replace(1,6,"melbourne",9);
  12.  
  13.     getch();
  14.     return 0;
  15.  
  16. }
can anyone tell me if i solved this correctly,
thankss
Nov 28 '06 #1
1 2793
horace1
1,510 Expert 1GB
hey,

i have to use the commands find and replace to replace all the occurrences of sydney to melbourne.

I have did this, but not sure if im on the right track:

Expand|Select|Wrap|Line Numbers
  1. #include <string.h>
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. string s1="sydney";
  11. cout << s1.replace(1,6,"melbourne",9);
  12.  
  13.     getch();
  14.     return 0;
  15.  
  16. }
can anyone tell me if i solved this correctly,
thankss
not quite, in C/C++ array indicies start from 0 so it should be
Expand|Select|Wrap|Line Numbers
  1. cout << s1.replace(0,6,"melbourne",9);
  2.  
see the following page for details of replace()
http://www.cppreference.com/cppstring/replace.html
Nov 28 '06 #2

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

Similar topics

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 =...
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...
3
by: Jim | last post by:
Hey all, I have a dilemma. I have a field in a table called "Supervisor." The table currently has 1,500+ records. Sometimes, the HR download that populates the field formats the field one way,...
3
by: asd987 | last post by:
Hi, Can anyone tell me if "replace" is supported by Access 97? I use the Dutch version and get the errormessage "sub or function not supported". Or is the Professional Edition needed? Thanks.
4
by: Dominique Deleris | last post by:
Hello, I desperately and urgently need a __working__ function, that will replace all occurences of a substring in a string. Prototype should be : char *str_replace(char *str, const char...
4
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String...
16
by: BBM | last post by:
This is so bizarre I hesitate to post it, but I need to get this working. My code looks like this... Private Const cmdTestResourcesSel = "SELECT * FROM TResources" & _ " WHERE Scenario =...
4
by: serge | last post by:
I managed to put together C# code and have it do the following: 1- Get all the table names that start with the letter "Z" from sysobjects of my SQL 2000 database and put these table names...
12
by: Michael | last post by:
In PHP there is a function called str_replace (http://php.net/str_replace). Basically you can freed in two strings and a "subject" string. Then it goes through the subject string searching for...
6
by: chromis | last post by:
Hi, I'm trying to remove line breaks from a user-inputted string and trying to do this using ereg_replace and a regular expression but struggling, hope someone can help! I would like to make...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.