I have greatly modified this code and I am now using some different methods.
My code now looks like this (a little messy, but I'll fix that in a bit):
Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "Console.h"
using namespace std;
int main(int argc, char *argv[])
{
string TEXT = "";
string SEARCHWORD = "";
int SKIP = 0;
int j = 0;
int g = 0;
int i = 0;
int printgrid = 0;
int count = 0;
namespace con = JadedHoboConsole;
cout<<con::fg_white;
cout<<"Input text to search: ";
cin>>TEXT;
cout<<endl;
cout<<"Input word to search for: ";
cin>>SEARCHWORD;
cout<<endl;
cout<<"Input skip value: ";
cin>>SKIP;
cout<<endl;
int RL = ((TEXT.length()) / SKIP);
string ROWS[RL];
string COLUMNS[SKIP];
const char* text = TEXT.c_str();
int SEARCHWORDPOSITIONS[5000];
int INSTANCE[SKIP];
while(g < 5000)
{SEARCHWORDPOSITIONS[g] = 0;
g++;}
while(j < SKIP)
{INSTANCE[j] = 0;
j++;}
g = 0;
j = 0;
// output the length of the text, rows, and columns
cout<<"Text length: "<<TEXT.length()<<" Rows: "<<RL<<" Columns: "<<SKIP<<endl<<endl;
// split the the text into rows
while((j < RL) && (g <= (TEXT.length() - SKIP)))
{
ROWS[j] = TEXT.substr(g,SKIP);
g = g + SKIP;
j++;
}
// split the text into columns
for(g = 0; g < RL; g++)
{
for(j = 0; j < SKIP; j++)
{
COLUMNS[j].append(ROWS[g].substr(j,1));
}
}
// find all instances of the search word
g = 0;
i = 0;
j = 0;
for(g = 0; g < SKIP; g++)
{
while((COLUMNS[g].find(SEARCHWORD,i)) != string::npos)
{
if(COLUMNS[g].find(SEARCHWORD,i) == 0)
{
SEARCHWORDPOSITIONS[j] = 1 * (g + 1);
}
else
{
SEARCHWORDPOSITIONS[j] = ((COLUMNS[g].find(SEARCHWORD,i)) * (g + 1));
}
i += (SEARCHWORD.length() + 1);
(INSTANCE[g])++;
++count;
j++;
}
}
cout<<endl;
j = 1;
for(g = 0; g < SKIP; g++)
{
if(count != 0)
{
printgrid = 1;
if(g == 0){cout<<SEARCHWORD<<" was found at: ";}
while(j <= count)
{
cout<<SEARCHWORDPOSITIONS[g]<<" ";
j++;
}
}
else
{
cout<<SEARCHWORD<<" was not found."<<endl;
}
}
cout<<endl<<endl;
i = 0;
j = 0;
if(printgrid == 1)
{
// print the grid to the screen
for(int g = 0; g < TEXT.length(); g++)
{
j = 0;
i = 0;
if(((g + 1) % SKIP) != 0)
{
while(j < count && i == 0)
{
for(int c = 0; c < SEARCHWORD.length(); c++)
{
if((SEARCHWORDPOSITIONS[j] + (c * SKIP)) == (g + 1))
{i = 1;}
}
j++;
}
if(i == 0)
{
cout<<TEXT[g];
}
else
{
cout<<con::fg_red<<con::bg_white<<TEXT[g]<<con::fg_white<<con::bg_black;
}
}
else
{
while(j < count && i == 0)
{
for(int c = 0; c < SEARCHWORD.length(); c++)
{
if((SEARCHWORDPOSITIONS[j] + (c * SKIP)) == (g + 1))
{i = 1;}
}
j++;
}
if(i == 0)
{
cout<<TEXT[g]<<endl;
}
else
{
cout<<con::fg_red<<con::bg_white<<TEXT[g]<<con::fg_white<<con::bg_black<<endl;
}
}
}
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
I would like to next have it read in the TEXT from a file instead of user input. That way I can search large portions of text without having to retype it.
I would also like to output the grid to a text file in order to easily view it. I would also like to be able to output the found words in a highlighted format like my code does now.
I am familiar with file input and output, but it has been forever since I have used them. Also, I am concerned with making the output grid formatted correctly.
Any suggestions on how to go about this next step?
Thanks.