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.
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.
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.
> 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.
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"
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' ;
}
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;
}
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 ) returnfalse ;
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 ) returntrue ;
elsereturnfalse ;
}
// 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 ) ;