searching problem (help needed asap)

Pages: 12345
> any suggestions

You already know how to read the contents of the file into a string.

Now use std::string::find() to find and print out the char offsets at which the token is found.
http://www.cplusplus.com/reference/string/string/find/
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int find_get(string filepath )
{
    ifstream file(filepath ) ;
    string line ;
    string get= "GET";

    typedef std::ifstream::pos_type pos_t ;

    pos_t file_pos = file.tellg() ;

    while( std::getline( file, line) )
    {
        size_t pos = 0 ;
        while( ( pos = line.find( get, pos ) )  != string::npos )
        {
            cout << file_pos + static_cast<pos_t>(pos+1) <<",";
            f_out << file_pos + static_cast<pos_t>(pos+1) <<",";
            pos += get.size() ; 
        }
        file_pos = file.tellg() ;

    }f_out<<endl;
    return 0;
}
Or, since the file is very small (8 lines or so):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <fstream>
#include <iterator>

std::string file_to_string( const std::string& path2file )
{
    std::ifstream file(path2file) ;
    std::istreambuf_iterator<char> begin(file), end ;
    return std::string( begin, end ) ;
}

// print out occurrances of string 'token' in file 'f_name'
void print_token_offsets( const std::string& f_name, const std::string& token )
{
    std::string str = file_to_string(f_name) ;

    std::cout << "token '" << token << "' found at char offsets:\n" ;
    for( std::size_t pos = str.find(token) ; pos != std::string::npos ;
          pos = str.find(token,pos) )
    {
        std::cout << pos+1 << ' ' ;
        ++pos ; // or pos += token.size()
    }
    std::cout << '\n' ;
}

int main() { print_token_offsets( "myfile.txt", "GET" ) ; }
thanks bro.. done.

a quick one. what if am to search for ANY 5 letter char that is immediatly repeated in the string.
like ( PINRTTTIINNNTTTTIPRINTPRINTPNNNNTRPPNNRI) print is immediately repeated in the string.


i would also like to know the position it occured and the letters reapeated.. thanks
Oh, c'mon. You should be able to do this on your own.

Iterate through the string, starting at position 0, picking up sub-strings of length 5 one by one and check if each sub-string is immediately repeated.

http://www.cplusplus.com/reference/string/string/substr/

Make an attempt, and if you have problems, post your code and a question.
@JLB dont know what am doing wrong. i get a debug assertion failed error when i used the code below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int repeat(string str)
{
	string str2 ;
		str2 = str.substr(6,6);
	for ( int i =0; i<str.length();i++)
	{
		
		if (str2[i] == str2[i +6])
		{
			cout <<str2<< i << endl;
		}
	}
	return 0;
}

and then i put in break point with this code btu still gives me the same error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int repeat(string str)
{
	string str2 ;
int count = 0;
		str2 = str.substr(6,6);
	for ( int i =0; i<str.length();i++)
	{
			if ( count == (str.length() - 6))
		{
			break;
		}
		if (str2[i] == str2[i +6])
		{
			cout <<str2<< i << endl;
		}
	}
	return 0;
}
Last edited on
Well if you're hitting the assertion without triggering the breakpoint, the obvious reason is that the assertion is happening before the breakpoint. That should help you narrow down where it's happening.

But if you're gettign an assertion, then it should be be really easy to use a debugger and find out exactly where the problem is occurring.
Last edited on
but is the above code right ?
> dont know what am doing wrong.
> i get a debug assertion failed error when i used the code below.

Ok, let's take this one baby step at a time.

First, implement the simple function

1
2
3
4
5
// returns true if
//     a. str.size() is > 9
//     b. the first five characters in str are repeated immediately
// else return false  
bool first_five_char_are_repeated( const std::string& str ) ;


Get it working, and then post the code. We canl then move on to the next step.
the first five characters in str are repeated immediately
// else return false


thats where the problem is.. writing the code.. dnt knw what code to use...
Ok, let's try something simpler.

1
2
3
4
5
6
std::string a_to_z = "abcdefghijklmnopqrstuvwxyz" ;

std::string a_to_e ;

// now get the a string containing first five characters in a_to_z and 
// assign it to a_to_e . print out a_to_e to verify that it contains "abcde" 


Use std::string::substr() http://www.cplusplus.com/reference/string/string/substr/
i get you very well. but the problem i have is that the 5 character string is unknown. i need to get any random 5 letter words that immediately repeats
Ok. So do this first (before attempting fancier stuff like 'any random 5 letter words').

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string str ;
std::cin >> str ; // get a string from the user

if( str.size() > 4 ) // if str has five or more characters
{
    std::string first_five ;
    
    // get the a string containing first five characters in str and 
    // assign it to first_five . 
    
    // .... your code here ....  
     
    // print out str and first_five to verify that 
    // first_five contains the first five characters of str
    std::cout << str << '\n' << first_five << '\n' ;
}


Post your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main()
{
string input;
const int MAX = 8;
char inputarr[MAX];
int inputsize = 0;
bool trig = true;
cout << " enter your characters";


while(trig == true)
{cin >>input  ;
	for (int i = 0;i < input.length() ; i++)
	{
		
		inputarr[i] = input[i];
		inputsize++;
		if (inputsize == MAX)
		{
			trig = false;
			break;
		}
	}
}
     inputarr[inputsize] = '\0';
	string str (inputarr);
	if (str.size() > 7)
	{
		string first_five = str.substr(5);
		cout << str << '\n' << first_five << '\n' ;
	}

return 0;
}


JLB.... this is what i got. but it only shows the last 3 letters
am i right wit it....
am i right wit it....


If you were right with it, it would display what you intended it to display.

http://www.cplusplus.com/reference/string/string/substr/

If you're unsure how something is used, look it up.

I'm not sure what you think you're doing with the char array and the copying, but it's completely unnecessary. You can just use the original string (and even if you wanted to use a new one string str(input); would've done the trick.) Also there was no reason to count the number of characters in the string since input.length() returns that directly.
Okay jlb i got to understand how the substring works now. so i moved on with my code and tried to find the repeated 5 letter word but am not sure of the code.. help me see whats wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int tand( string listifstring)
{
	
	string first_five = listifstring.substr(0,5);
	string next_five = listifstring.substr(5,5);
	for (int n = 0; n <listifstring.length(); n++)
	{
		if ( first_five[n] == next_five[n+1])
		{
			cout<< first_five << next_five << " found at " << n << endl;
	
		}
	}

	return 0;
}
i keep getting a debug assertion failure and its not working
To check id two std::string objects are equal, use the == operator.
if( str1 == str2 ) { /* str1 and str2 are equal */ }

So, the function can be written as
1
2
3
4
5
6
7
8
9
10
11
12
// returns true if
//     a. str.size() is > 9
//     b. the first five characters in str are repeated immediately
// else return false  
bool first_five_char_are_repeated( const std::string& str )
{
    if( str.size() < 10 ) return false ;
    std::string str1 = str.substr( 0, 5 ) ; // the first five characters
    std::string str2 = str.substr( 5, 5 ) ; // the next five characters
    if( str1 == str2 ) return true ;
    else return false ;
}


Or simply:
1
2
bool first_five_char_are_repeated( const std::string& str )
{ return ( str.size() > 9 ) && ( str.substr( 0, 5 ) == str.substr( 5, 5 ) ) ;


Now, write this function:
1
2
3
4
5
// returns true if
//     a. str.size() is > ( pos + 9 )
//     b. the five characters in str starting at position pos are repeated 
// else return false  
bool five_chars_are_repeated( const std::string& str, std::size_t pos ) ;


And post your code.
1
2
3
4
5
6
7
8
9
10
11
bool tand( string & temp_file_arr)
{
	size_t pos;
	if( temp_file_arr.size() < pos + 9 ) return false ;
	string first_five = temp_file_arr.substr(0,5);
	string next_five = temp_file_arr.substr(5,5);
	if( first_five == next_five ) return true ;
    else return false ;

	return 0;
}



thats my code.. what next sir?
Pages: 12345