+ 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; }

1st Nov 2020, 5:30 PM
00xSec
00xSec - avatar
3 odpowiedzi
+ 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...
1st Nov 2020, 5:47 PM
Ameer Haider
Ameer Haider - avatar
0
ok thank you
1st Nov 2020, 5:50 PM
00xSec
00xSec - avatar
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"; } }
2nd Nov 2020, 12:56 PM
00xSec
00xSec - avatar