Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String comparing 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i am using fstream to open a file, and replace every occurance of the word "AN" with the word "AND." I am confused about this and i was wondering if anyone had any ideas/examples that could help me...
 
hmm, I dont know the comand off the top of my head , I think you can do a combination of strcmp, but i cant remeber the replace command, are we assuming there is always going to be a space after "AN", because I can see a problem where it'll see the "AN" inside of an already existing AND, and create an "ANDD"


I do know that strcmp will compare two strings, I guess one approach would be to loop through the string, at 3 letter intervals, like checking the first letter with length of three, then next and so forth.

like in
"Hungry Hungry Hippos"
it compares
"AN " to "Hun" and "ung" and "ngr" and ... well you get the point.

need more information on just the comparism part? ( I know once you can find a match, you can just remeber the index in the character array its at and just cut and paste around that via code ) Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
On a side note... look into the Boyer-Moore algorithm. It is quite effective for string comparisons like this.

Matt
 
Another thought, if you are using fstream to do this, why not do this

int main()
{
ifstream input_stream("filename");
char buffer[256];//ya, a bit large

while(ifstream.peek()!=EOF)
{
input_stream>>buffer;
if( !strcmp(buffer,"AN") )//THIS IS CASE SENSITIVE
strcpy(buffer,"AND");

// do your replace stuff here with buffer
}
}


 
LOL... im sorry bout this... every time I hit post another idea comes to mind.

the function strstr will search for a substring and return a pointer to the beggining of that string if it exists. Look into this function as well

Matt
 
hmm, kind of like instr in VB, also Zyrenthian, while your method is still correct in a sense, without searching for "AN " , it could replace "AND" with "ANDD" as mentioned earlier. Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Commenting on kb244's posts, this is why one should always search for "AN ", so that it will not replace any instance of an with and. Other places you would have a problem are:
any
instance
random
sand
bland
To provide a short list, anyhow. But you see my point, right? So search for "AN " instead of "AN".
 
I'm wondering if this data he's working with, its something more than english words (perhaps, but understanding what we both mentioned, is a good idea) and used "An" -> "And" as an example. to get answers for what he needs. Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Yes, I know, but what I stated holds true. When comparing and look for specific "words" it is a good idea to leave something either in front of or behind the data in the compare field to ensure that only the desired data is checked. If you know a space will precede and follow the data, why not check for " DATA "? It will only reduce errors.
 
if you use std::string there are much easier functions:
#include<string>
#include<iostream>
using namespace std;
int main()
{
string x;
x=&quot;hello my world&quot;;
x.replace(x.find(&quot;my&quot;),2,&quot;your&quot;);
cout<<x.c_str()<<endl;
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top