+ 1
why it's not working "deja vu " challenge
#include <iostream> using namespace std; int main() { string word = "asd"; bool found = false; // getline(cin, word); for (int i = 0; i <= word.length() ; i++) { for (int j = 0 ; j <= word.length() ; j++) { if (word[i] == word[j]) { found = true; break; } } if (found ) { break ; } } if (found ) { cout << "Deja Vu" ; } else { cout << "Unique"; } return 0; }
3 ответов
+ 1
You should write second for loop like this :
for (j=i+1 ; j<word.length; j++)
Because if you initialize j with 0 then in the if condition, (word[0] == word[0]) always match each other. But we want to test word[0] == word[1] , up until word.length.
Hopefully this makes some sense...
0
ok thank you
0
i solve it
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string word;
bool found;
getline(cin, word);
for (int i = 0 ; i < word.length() ; i++)
{
for (int j = 0 ; j < word.length() ; j++)
{
if (i == j)
{
continue;
}
else if (word[i] == word[j])
{
found = true;
break;
}
}
}
if(found)
{
cout << "Deja Vu";
}
else
{
cout << "Unique";
}
}