Forums - Open Redstone Engineers
c++ String searching goodness - Printable Version

+- Forums - Open Redstone Engineers (https://forum.openredstone.org)
+-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html)
+--- Forum: Programming (https://forum.openredstone.org/forum-8.html)
+--- Thread: c++ String searching goodness (/thread-5972.html)



c++ String searching goodness - greatgamer34 - 03-13-2015

Hey guys, i've devised a cool way to search for a keyword in a string in C++. Just compile and run. Big Grin

PHP Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>

using namespace std;

int main(void)
{
 
int a,i,z,loc,pos;
 
string inputFileName;
 
string s,term;
 
ifstream fileIn;
 
char ch;
 
cout<<endl;
 
cout<<endl;
 
cout<<"Enter the name of a file of characters: ";
 
cin>>inputFileName;
 
cout<<endl;
 
cout<<endl;
 
fileIn.open(inputFileName.data());
 
assert(fileIn.is_open() );
 
i=0;
 while (!(
fileIn.eof()))
  {
ch=fileIn.get();
   
s.insert(i,1,ch);     //insert character at position i
   
i++;
  }
cout<<s;
cout<<endl;
cout<<endl;
cout<<"Enter a word/phrase you want to search for in the file you entered: ";
cin>>term;
cout<<"The word/phrase  *"<< term <<"*  will have '***' before it and after it"<<endl;
cout<<endl;
= (int)term.length();

loc 0;

while ((
loc s.find(termloc)) != std::string::npos)
{
    
// check for space at start of term, or check
    // for beginning of string
    
if (loc != && s[loc 1] != ' ')
    {
        
loc += z;
        continue;
    }

    
// check for space at end of term, or check for end of string
    
if (loc != (s.length() - z) && s[loc z] != ' ')
    {
        
loc += z;
        continue;
    }

    
s.insert(loc1'*');
    
s.insert(loc+11'*');
    
s.insert(loc+21'*');

    
s.insert(loc+3+z1'*');
    
s.insert(loc+4+z1'*');
    
s.insert(loc+5+z1'*');

    
loc += z;
    
loc += 6// the amount of asterisks added
}
cout<<s;
 return 
0;




RE: c++ String searching goodness - greatgamer34 - 03-22-2015

Does anyone like this? :/


RE: c++ String searching goodness - PabloDons - 03-22-2015

well I don't speak C++, but in python it is simply like this:
Code:
>>> substring = "py"
>>> string = "python"
>>> substring in string
True
or am I missing something completely obvious?


RE: c++ String searching goodness - tyler569 - 03-27-2015

Unless I'm cracra it doesn't seem to do anything
Code:
�tyler@mac:~ % ./a.out


Enter the name of a file of characters: file


this is
a file
of charachters


Enter a word/phrase you want to search for in the file you entered: fi
The word/phrase  *fi*  will have '***' before it and after it

this is
a file
of charachters
�tyler@mac:~ %

Also the programming style is..... interesting, to say the least (imo).


RE: c++ String searching goodness - greatgamer34 - 03-28-2015

fi isn't a word or phrase found in the file... try searching for a full word that is there.


RE: c++ String searching goodness - Treesin - 04-04-2015

(03-13-2015, 05:34 PM)greatgamer34 Wrote: Hey guys, i've devised a cool way to search for a keyword in a string in C++. Just compile and run. Big Grin


PHP Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>

using namespace std;

int main(void)
{
 
int a,i,z,loc,pos;
 
string inputFileName;
 
string s,term;
 
ifstream fileIn;
 
char ch;
 
cout<<endl;
 
cout<<endl;
 
cout<<"Enter the name of a file of characters: ";
 
cin>>inputFileName;
 
cout<<endl;
 
cout<<endl;
 
fileIn.open(inputFileName.data());
 
assert(fileIn.is_open() );
 
i=0;
 while (!(
fileIn.eof()))
 
 {ch=fileIn.get();
 
  s.insert(i,1,ch);     //insert character at position i
 
  i++;
 
 }
cout<<s;
cout<<endl;
cout<<endl;
cout<<"Enter a word/phrase you want to search for in the file you entered: ";
cin>>term;
cout<<"The word/phrase  *"<< term <<"*  will have '***' before it and after it"<<endl;
cout<<endl;
= (int)term.length();

loc 0;

while ((
loc s.find(termloc)) != std::string::npos)
{
 
   // check for space at start of term, or check
 
   // for beginning of string
 
   if (loc != && s[loc 1] != ' ')
 
   {
 
       loc += z;
 
       continue;
 
   }

 
   // check for space at end of term, or check for end of string
 
   if (loc != (s.length() - z) && s[loc z] != ' ')
 
   {
 
       loc += z;
 
       continue;
 
   }

 
   s.insert(loc1'*');
 
   s.insert(loc+11'*');
 
   s.insert(loc+21'*');

 
   s.insert(loc+3+z1'*');
 
   s.insert(loc+4+z1'*');
 
   s.insert(loc+5+z1'*');

 
   loc += z;
 
   loc += 6// the amount of asterisks added
}
cout<<s;
 return 
0;



How about
Code:
#include <iostream>

#include <fstream>
#include <string>
#include <cstdio>

using namespace std;

string mark(string text,int startPos, int len){
    string toReturn = text;
    toReturn.insert(startPos,"***");
    toReturn.insert(startPos+len+3,"***");
    return toReturn;
}

int main(void){

    cout<<"~~~~~~~~~~~~~~"<< endl
        <<"Keyword Finder"<< endl
        <<"~~~~~~~~~~~~~~"<< endl;

    //Get user preferences
    string keyword = "ban";
    int lenKeyword;

   
    lenKeyword = keyword.size();
    string keywordPadded = " "+keyword+" ";
    
    //Read in data
    string fullText = "ban bananas, they are bannable when ban bands ban the bans ban";

    cout<<"File Text: "<< fullText << endl;
    cout<<fullText.size()<<endl;

    //iterate through each potential match and mark it
    for(int i = fullText.find(keywordPadded); i < fullText.size(); i = fullText.find(keywordPadded,i+lenKeyword)){
        cout << i << endl;
        fullText = mark(fullText,i+1,lenKeyword);
    }

    //check edge cases
    if(fullText.find(keyword+" ")==0)
        fullText = mark(fullText,0,lenKeyword);

    if(fullText.find(" "+keyword,fullText.size()-lenKeyword-1)==fullText.size()-lenKeyword-1)
        fullText = mark(fullText,fullText.size()-lenKeyword,lenKeyword);

    cout<<"Highlighted Text: "<< fullText << endl;
    return 0;
}

(Didn't fuck around with file I/O because I was using an online compiler, should be simple to implement though)