Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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: 


C++
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
ifstream myfile_in ("sample.txt");
ofstream myfile_out ("Lista2.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;
}
Posted

1 solution

You are going to have to look at the problem a little harder: the "brute force and ignorance" approach you are trying to use is only really useful for a small number of values - you have a lot of them: zero (and it's synonyms), one, two...nineteen, twenty, thirty..ninety, hundred, thousand, million, and so forth, plus they may be interrupted by line breaks, punctuation, the word "and", and so forth. You should consider these as well.

The way I would do it is to parse the whole input file into words, and then "tokenise" the words to "number", "punctuation", "conjunction", "not a number" and so on, then process the tokens looking for viable number combinations. Then write out the result.
Otherwise "twenty-seven" will be converted to "#-#", "and no-one here will" will become "#-# hear will", and "one thousand, eight hundred and thirty-six and no pence" too at best "#, #-# no pence"

It's not a task that benefits from a brute force approach, and no-one here is going to just hand you the code to do it!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900