473,387 Members | 1,542 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.

Compare text and replace!

stealwings
I have a little problem with my program, or maybe it is not that little, anyway here is the code:
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. #include <fstream>
  5. using std::ifstream;
  6. using std::ofstream;
  7. #include <stdio.h>
  8. #include <cstdlib>
  9. #include <string>
  10. #include <stdlib.h>
  11. #include <iomanip>
  12. #include <vector>
  13. #define MAXLENGTH 256
  14. int _tmain(int argc, _TCHAR* argv[])
  15. {
  16.     char MyWord[MAXLENGTH];            //Temp array to save input file words.
  17.     char MyStandard[MAXLENGTH];        //Temp array to save the standard words list
  18. //    char *MyString;
  19.     vector<string> MyWords;            // Vector to hold input file.
  20.     vector<string> Standard;        // Vector to hold Text-normalization-phrases
  21.  
  22. //    string FileName;
  23.     string original;
  24.     string replace;
  25.     ifstream ReadWord;
  26.     ifstream ReadStandard;
  27.     ofstream WriteWord;
  28. //    cout<<"imput file name plz"<<endl;
  29. //    cin>>FileName;
  30.     ReadWord.open("file.txt");
  31.     WriteWord.open("file.dat");
  32.  
  33.     ReadStandard.open("Standard.txt");    
  34.     if (!ReadStandard)
  35.     {
  36.         cerr<<"Standard text file not found."<<endl;    
  37.         exit (1);
  38.     }
  39.  
  40.     while(!ReadStandard.eof())                        //read the file till reaches the end.
  41.     {
  42.  
  43.         ReadStandard.getline(MyStandard,MAXLENGTH);    // read a line from the file.(one line is a phrase)
  44.         Standard.push_back(MyStandard);                //push the phrase into the vector "Standard";
  45.  
  46.     }                                                //read standard words over
  47.     ReadStandard.close();                            //close the file handle;
  48.  
  49.     if(!WriteWord.is_open())
  50.     {
  51.         cerr<<"couldn't create file"<<endl;
  52.         exit(1);
  53.     }
  54.     if (!ReadWord)
  55.     {
  56.         cerr<<"not found1"<<endl;
  57.         exit (1);
  58.     }
  59.  
  60.     while(!ReadWord.eof())
  61.     {
  62.  
  63.         ReadWord.getline(MyWord,MAXLENGTH);
  64.         MyWords.push_back(MyWord);
  65.  
  66.     }
  67.     ReadWord.close();
  68. //    WriteWord.close();
  69.  
  70.  
  71.     for(unsigned i=0;i<MyWords.size();i++) 
  72.     {
  73.  
  74.         for(unsigned j=0;j<Standard.size();j++)  //each MyWords compare to the Standard and see if it is in the standard vector
  75.         {
  76.             if(MyWords[i]==Standard[j])            // if it equal to one of the words in the standard vector 
  77.                 MyWords[i]=MyWords[i]+"/";        // add the slash to the end;
  78.         }
  79.  
  80.         cout<<MyWords[i]<<endl;
  81.         i++;
  82.     }
  83.  
  84.     for(unsigned j=0;j<MyWords.size();j++)        // for each word in MyWords vector  
  85.     {
  86.         WriteWord<<MyWords[j]<<endl;            // write it to the destination file
  87.     }
  88.     WriteWord.flush();                            // flush the memory to ensure all is written to the file
  89.     WriteWord.close();                            // close the write file handls
  90.     return 0;
  91. }
This code is use to compare an input text file which is named as file.txt with a standard text file (Standar.txt), and see if the input file text matches with my standards, if matches then it will add an "/" after each match. But only if they are exactly the same, i.e there is a phrase "hello every one" in both texts it will write out "hello ever one/" after the process. But what I want to do is that if there is a phrase "hi, hello everyone" and in my standard I have the phrase "hello everyone" it will compared them and the write out "hi, hello everyone/". Can some one give me some clues of how can I make it work like that? Thx in advance.
Mar 1 '07 #1
7 3113
Banfa
9,065 Expert Mod 8TB
look up the C string standard library functions, specifically strstr which finds the occurance of 1 string inside another.
Mar 1 '07 #2
Thx, I already fix my problem with the find function, but I'm not sure whether if it's the best way to do it so, can some of the experts plz check out my code and tell me if there is a better way to do what I have to do, thx in advance.THIS IS THE PART OF THE CODE THAT DOES THE SEARCH, COMPARE AND REPLACE.
Expand|Select|Wrap|Line Numbers
  1. //SEARCH AND COMPARE, THEN REPLACE IF MATCH IS TRUE.
  2.     for(unsigned i=0;i<MyWords.size();i++) 
  3.     {
  4.  
  5.         for(unsigned j=0;j<Standard.size();j++)  //each MyWords compare to the Standard and see if it is in the standard vector
  6.         {
  7.             string::size_type FoundAt=MyWords[i].find(Standard[j]);
  8.             while( string::npos != FoundAt )
  9.                 {
  10.                     MyWords[i].replace( FoundAt, Standard[j].length(),Standard[j]+"/");
  11.                     cout<< MyWords[i]<< endl;
  12.                     FoundAt = MyWords[i].find( Standard[j], FoundAt + Standard[j].length() );
  13.                 }
  14.  
  15.             //if(MyWords[i]==Standard[j])            // if it equal to one of the words in the standard vector 
  16.             //    MyWords[i]=MyWords[i]+"/";            // add the slash to the end;
  17.         }
  18.  
  19.     }
Mar 2 '07 #3
Hello everyone;
I have a new question about this program that I couldn't solve myself yet. In the last post the function I use to search for my required word is too slow, takes at least a hundred years to finish comparing a large text, since it does a linear search. I know that using binary search it will speed up much much more, but here is where problem appears. To do a binary search I will need to cut the sentences of my input file into words then compare them with my Sorted Array. Well, the problem is that my sorted array does not only consist of words, but phrases, so I will have to take one word from the input and see if there is a match, if there is a match then take the second one and do the same until it reaches to the same phrase. Now my question is how do I do that, how do I take one word, compare it, and then take the second, and the third.....and so on without deleting the previous ones until I meet up with a the same phrase of my sorted array. Can some one give me some hints plz.

Thanks in advance!!!!!!
Mar 15 '07 #4
Sorry if the last post for my question was too long, maybe a little example of what I'm looking for will be easier to be understand.
e.g. Lets say I have a sentences "This world is full of beauty!" and I have in my sorted array the phrase "full of beauty", I would like to take each word of the sentence to and compare it with my array like the following:
1. Take the word "This", since it doesn't match delete it
2. Take the word "world", since it doesn't match delete it.
3. and so on until "full", since "full" is part of my phrase "full of beauty" conserve it and take the next and compare it, and so on till I get the full phrase "full of beauty" then return that the phrase was found.

Please help me out, this question is driving me crazy. Thank in advance! And sorry for all the trouble.
Mar 15 '07 #5
There isn't any body who can give me hand in this problem yet? Or shall I star a new discussions for my question to be answered?
Mar 21 '07 #6
DeMan
1,806 1GB
This has now (at least partly) been answered in your other post....
Mar 21 '07 #7
This has now (at least partly) been answered in your other post....
Yes, thank you DeMan :)
Mar 21 '07 #8

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

Similar topics

4
by: alexis | last post by:
Hi, In a form I have the curent date <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>"> and <input type=text name="datebox" size=15> The date format is d/m/Y...
16
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
9
by: geronimo_me | last post by:
Hi, I am atempting to compare part of a field with the whole of another field in access. Is this possible? Basically I have 2 tables with the following info: Table1 Field1 = MR.
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
6
by: Justin Fancy | last post by:
Hi Everyone, I'm lookin for a very confusing loop (to me), to compare two files. Here it is. I have two arrays with paths stored in both. example: /en/aviation/you.htm. I need to search array...
33
by: joemo2003 | last post by:
I try to compare the text in excel and visio, if the text match, then replace that visio text with a range of excel text. Do anybody have any experiment with that? any ideal will be a big help. ...
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
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:
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...
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
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...
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,...

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.