Replace the text numbers string in a txt file using C++.. Help Me..

For example:
Before Document:
hai hello my daily salary is two thousand and five and your salary is five billion. my age is twenty-five.

After Document:
hai hello my daily salary is # and your salary is #. my age is #.

All the text numbers and i put the # symbol.. Please Give me the Correct Code..

I am trying this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
ifstream myfile_in ("sample.txt");
ofstream myfile_out ("output.txt");
string line;
 
void find_and_replace( string &source,  string find, string replace ) { 
 
    size_t j;
 
    for ( ; (j = source.find( find )) != string::npos ; ) {
        source.replace( j, find.length(), replace );
        }
 
        myfile_out << source <<endl;
        cout << source << endl;
        }
 
int main () {
 
  if (myfile_in.is_open())
  {
    int i = 0,j;
    //string strcomma ;
   // string strspace ;
 
    while (! myfile_in.eof() )
    {
 
      getline (myfile_in,line);
 
	  string strcomma= "two";
	  string strspace = "#";
	
 
      find_and_replace( line , strcomma , strspace );
  
   
      i++;
 
    }
 
    myfile_in.close();
 
  }
 
  else cout << "Unable to open file(s) ";
 
  system("PAUSE");
  return 0;
}
The easy way to do this is to come up with a number of easier steps to follow, then to code each step, and then put the steps together. Check all the difficult cases (eg: two hundred and six) (eg a hundred and six)

eg
Read words from file into a list of words (std::list<std::string>)
Loop through the list and replace all recognized numbers with "#"
Loop through the list and replace all sequences "#", "and", "#" with "#"
Loop through the list and replace all repeated "#"es with a single "#"
Loop through the list and write to file.
Last edited on
Topic archived. No new replies allowed.