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

string occurences

Hi,

im doing this question:
Replace all the occurrences of a string in a file and report number of replacements.

I did the first part of the question, but im having problems with the second part, i which i have to report the number of replacements.

this is the code that i did so far:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     string whole_file, str;
  10.     ifstream in ("file.txt");
  11.     while (!in.eof())
  12.     {
  13.           std::getline(in,str,'\n');
  14.           whole_file = whole_file + str + "\n";
  15.  
  16.     }
  17.  
  18.   int pos = 0;
  19.  
  20.   while (pos!=-1 && pos<whole_file.length())
  21.   {
  22.     pos = whole_file.find ("money", 0);
  23.     if (pos!=-1)
  24.     {
  25.       cout<<whole_file.replace(pos, 5, "university", 10);
  26. }
  27. }
  28. cout<<whole_file;
  29.  
  30. getch();
  31. return 0;
  32.  
  33. }

Thanks..... all help on how i can solve the second part of the question will be very much appreaciated.
Dec 8 '06 #1
2 1178
horace1
1,510 Expert 1GB
all you need is a counter which you increment when you replace a string, e.g.
Expand|Select|Wrap|Line Numbers
  1.   int pos = 0, counter=0;
  2.  
  3.   while (pos!=-1 && pos<whole_file.length())
  4.   {
  5.     pos = whole_file.find ("money", 0);
  6.     if (pos!=-1)
  7.     {
  8.       cout<<whole_file.replace(pos, 5, "university", 10);
  9.       counter++;
  10. }
  11. }
  12.  
Dec 8 '06 #2
all you need is a counter which you increment when you replace a string, e.g.
Expand|Select|Wrap|Line Numbers
  1.   int pos = 0, counter=0;
  2.  
  3.   while (pos!=-1 && pos<whole_file.length())
  4.   {
  5.     pos = whole_file.find ("money", 0);
  6.     if (pos!=-1)
  7.     {
  8.       cout<<whole_file.replace(pos, 5, "university", 10);
  9.       counter++;
  10. }
  11. }
  12.  

Thanks soooo much for your help horace!! :D
Dec 8 '06 #3

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

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
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: Ralph Noble | last post by:
Does anyone know of a string function in Access that will allow me to count the number of instances one string occurs within another? Or if there is some sort of word count function? If there is,...
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 =...
8
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some...
2
by: kasala | last post by:
I get an xml document as input from other department. The input xml document i recieve has a particular word "rnx" which should not be there and my system doesn't support it. And there is also...
1
by: Alexnb | last post by:
Basically I want the code to be able to pick out how many strings there are and then do something with each, or the number. When I say string I mean how many "strings" are in the string "string...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.