searching problem (help needed asap)

Pages: 12345
@jumper 007 thanks alot but for now am very new to C++ and i think my brain is too mild for ur alog style. I need more sutle ways yo go abt solving c++ probs first then i'll upgrad to ur level. At least i must learn to walk before i can fly. Would keep in contact with you sir. Thanks a lot
Let str be "abcd12345yz" (11 chars) and pos be 16.
Is a check for string length required?

Let str be "abcd1234512345z" (15 chars) and pos be 4.
What should the check for string length be?


pos is initially zero. but looking at it the counter increase it as it searches thru the string.

Let str be "abcd1234512345xyz", and pos be 4
Which are the two substrings to be compared?
At what position does the second substring start? Is it pos+1 (4+1)?


here the first substring starts from 0 to 5 and since pos is 0. then the next sub should be pos+5 right
got it.... ****dancing*****


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool nextfive( string & file_arr, size_t pos )
{
	if( temp_file_arr.size() < 9 ) return false ;
	string first_five = file_arr.substr(pos,5);
	string next_five = file_arr.substr(pos+5,5);
	if (first_five ==next_five)  return true ;
    else return false ;
}
	
bool tand( string & file_arr)
{
	for (int  p =0; p < file_arr.length() -9; p++)
	{
		if (nextfive(file_arr, p))
		{  
	            cout<<file_arr.substr(p, 5)<<" "<<file_arr.substr(p+5, 5)<< " found at "  << p <<endl;
		}  
	} 	
	return 0;
}

Last edited on
at last it wasnt necessary to use both functions

1
2
3
4
5
6
7
8
9
10
11
bool tand( string  file_arr)
{
	for (int  p =0; p < file_arr.length() -9; p++)
	{
		if (file_arr.substr(p,5) == file_arr.substr(p+5,5))
		{  
	            cout<<file_arr.substr(p, 5)<<" "<<file_arr.substr(p+5, 5)<< " found at "  << p <<endl;
		}  
	} 	
	return 0;
}

Last edited on
now ow do you print out a word in reverse. like christmas becomes samtsirhc ?
@werlay Sir, I have to tell you that I've been in your situation exactly one year ago. Our teacher gave us as an assignment almost the same task. Most of my classmates didn't solve it, because they tried to use a lot of complicated things. I, instead, used the KMP algorithm for pattern matching. Not only did I get a 10, but also my teacher congratulated me on using the easiest/most optimal way possible.
Comparing the two methods, the KMP, and your method, in my opinion, the KMP is more easier to understand. If you wish, as I said before, I can give you full explanations on the KMP, or if you wish, I can guide you through the same tutorials I've used when I first learnt this algorithm.
At least at my school, using already defined algorithms is compulsory. And by learning those algorithms, you actually understand what their "inventor" was thinking of, what his idea was, and you finally develop a good algorithmic thinking, being able to solve further problems without difficulties.

I really hope I could help you,
~ Raul ~

EDIT: http://www.cplusplus.com/reference/string/string/reserve/
Last edited on
@jumper--thanks alot.would surely keep in touch.

EDIT: http://www.cplusplus.com/reference/string/string/reserve/


i wanna know about reverse not reserve
@JLB and CIRE. thanks guys. still waiting for you on tutorial about reversing a string.
Oh sorry. My fault. Clicked the wrong link.
This is the syntax:

1
2
3
4
5
6
7
8
9
#include <string>
#include <algorithm>

// code

string s1;
reverse(s1.begin(), s1.end());

//code 


EDIT: You can also create your own reverse function using pointers if you want. It would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void stringReverse(char *s){
    int i, j;
    char c;
     
    i = 0;
    j = strlen(s)-1;
     
    while (i < j){
        c = s[i];
        s[i] = s[j];
        s[j] = c;
         
        i++; j--;
    }
}
Last edited on
@jumper i did something like this.
1
2
3
4
5
6
void rev ( string s1)// str1 has abt 10 letters
{
string s2 = s1.substr(0,6); //getting the first six letters
string s3 = reverse(s2.begin(), s2.end());  reversing the first 6 letters
 cout << s1 << " " << s3 ;
}




problem is i get errors
Yes.
string s3 = reverse(s2.begin(), s2.end()); // WRONG

Correct:

1
2
3
string s3;
reverse(s2.begin(),s2.end());
s3.assign(s2);


~ Raul ~

EDIT: Function reverse doesn't return a value. If you want to pass the reversed string to another one, you have to use the function assign.
Last edited on
One way to reverse a sequence of length n { a0, a1, a2, ..., an-2, an-1 } is:

swap a0 with an-1
swap a1 with an-2
and so on till we reach the middle of the sequence.
@JLBorges... That's what I said one post before:

@Me: EDIT: You can also create your own reverse function using pointers if you want. It would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void stringReverse(char *s){
    int i, j;
    char c;
     
    i = 0;
    j = strlen(s)-1;
     
    while (i < j){
        c = s[i];
        s[i] = s[j];
        s[j] = c;
         
        i++; j--;
    }
}


~ Raul ~
Last edited on
> @JLBorges... That's what I said one post before:

Yes.

I was encouraging werlay to think about the general case 'reverse a mutable sequence in place'.
Rather than focus on a special case 'reverse a string'.
@jumper

1
2
3
4
string s2;
string s3;
reverse(s2.begin(),s2.end());
s3.assign(s2);



s2 did not reverse. s3 still has the same string as s2 after the assign function
problem about your other technique is it works with chars, and i wanna reverse a string. so i gotta convert to char first?
@jumper, @JLB @Cire still waiting on you guys
What are you waiting for?

1. Implement reverse (as per the logic given here) http://www.cplusplus.com/forum/general/88575/4/#msg477148

2. Compile the code; fix the compile time errors if any.

3. Once it compiles, run it and test it.

4. If it does not work , modify the code and go back to step 2.

If you encounter problems, post the code and ask a question.
that means i'll have to change my strings to reverse into chars, right?

what am trying to do is like the previous problem, i wanna check if

1
2
if (file_arr.substr(p,5) == file_arr.substr(p+5,5)
// where file_arr.substr(p+5,5) is the reverse of file_arr.substr(p,5) 


you get?
Last edited on
> that means i'll have to change my strings to reverse into chars, right?

A string is a sequence of chars. If you know how to reverse a sequence, you would be able to reverse a string.


> what am trying to do is

It can be done in many ways.

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
bool check_it1( const std::string& file_arr, std::size_t p )
{
    if ( file_arr.size() < (p+9) ) return false ;
    auto begin1 = file_arr.begin() + p ; 
    auto begin2 = file_arr.rbegin() + file_arr.size() - (p+10) ; 
    return std::equal( begin1, begin1+5, begin2 ) ;

}

bool check_it2( const std::string& file_arr, std::size_t p )
{
    if ( file_arr.size() < (p+9) ) return false ;
    auto substr1 = file_arr.substr( p, 5 ) ;
    auto substr2 = file_arr.substr( p+5, 5 ) ;
    return substr1 == std::string( substr2.rbegin(), substr2.rend() ) ;
}

bool check_it3( const std::string& file_arr, std::size_t p )
{
    if ( file_arr.size() < (p+9) ) return false ;
    auto substr1 = file_arr.substr( p, 5 ) ;
    auto substr2 = file_arr.substr( p+5, 5 ) ;
    std::reverse( substr2.begin(), substr2.end() ) ;
    return substr1 == substr2 ;
}


But the important issue is: do you clearly understand what these functions are doing?
Pages: 12345