Deja Vu what is wrong? Will not pass last test | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Deja Vu what is wrong? Will not pass last test

#include <iostream> using namespace std; int main() { string input; getline(cin, input); int input_length = input.length(); int i; int j; char a; char b; string z; for(i=0; i<=input_length; i++) { a=input[i]; for (j=0; j<=input_length; j++) b=input[j]; if (input[i]!=input[j] && a==b) z="Deja Vu"; if (a!=b) z="Unique"; } cout<<z; return 0; }

15th Feb 2020, 11:40 PM
kevin
kevin - avatar
10 Answers
+ 1
Salam, hi 1st, if you want to check evry char from a string with n char, you have to start index from 0 to n-1 (i = 0: i < n). Thats to avoid geting index out of area. 2nd, in if statement input[i] and a are the same even input[j] and b are the same so its like you do (a == b && a != b), its always false. 3rd, if you correct the previous mistakes(1st and 2nd), the nested for doesn't guard the two if statements, thats mean you are comparing only the last char from the 2nd string with each char in the 1st string. 4th, when the code runs, the two 'if' statement in the loop will be checked evry sequence, thats mean if the condition in the first 'if' true, the statement (z = "Deja Vu") will be excuted then if the condition of the 2nd 'if' true in the folowing sequence then the statement (z = "Unique") will be excuted, that will delate the previous assignement.
16th Feb 2020, 7:18 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 2
#include <iostream> using namespace std; int main() { string input; getline(cin, input); int input_length = input.length(); int i; int j; char a; char b; string z; z="Unique"; for(i=0; i<input_length; i++) { a=input[i]; for (j=i+1; j<input_length; j++) { b=input[j]; if (a==b) z="Deja Vu"; } } cout<<z; return 0; }
17th Feb 2020, 6:15 PM
kevin
kevin - avatar
0
I dont know what the task is but first of all it looks like an Index out of bounds cause the condition of you for looks also allows the length of the input as an index which would mean that input has input.lenght + 1 element cause index starts at 0. Thats why it doesnt make sense to use <= and you should use < instead
16th Feb 2020, 2:06 AM
Jnn
Jnn - avatar
0
Jnn, The purpose of the program is to test for duplicate letters within a string. So my thought was to use a for loop nest. My problem is I cant get the program to find letter right next to one another like "andkeeehy" it cant tell that the letter "e" duplicates. it can however find letters separated in the string like "ahgjfkda" it sees the letter "a" and outputs deja vu. If I do as you said and take out the = sign it only passes 2 cases that need the "unique" output. It doesn't pass any case that needs the "deja vu" output. I feel like I am close just not understanding what I am doing vs. What I need to do.
16th Feb 2020, 3:46 AM
kevin
kevin - avatar
0
In first if condition, input[i]! =input[j] is same as a! =b So you are doing like a!=b && a==b never be true... And <= causes overflow. Take < only..
16th Feb 2020, 9:54 AM
Jayakrishna 🇮🇳
0
So how do you compare input[1] to input[2] , and input [2] to input [3]. If i compare input [1] to input [1] it is going to output "deja Vu" and has only seen a letter once. So that has to be stated somewhere. Thats what i thought i was doing with input[i] and input[j]. Originally i just called out ( i != j ) but it gave errors Maybe at the beginning i should go j =i++; Not sure ill have to play around some more. I understand the < symobl.
16th Feb 2020, 2:34 PM
kevin
kevin - avatar
0
i != j is the correct statement. You already assigned input[i] to a, so input[i] is the value while i is the index value. Else you can start j=i+1 directly so need to check only a==b only.. If match found then break loop for that....
16th Feb 2020, 2:41 PM
Jayakrishna 🇮🇳
0
Ok that makes sense. I will try it in my free time today and give update with my findings.
16th Feb 2020, 2:47 PM
kevin
kevin - avatar
0
So, you can make one simple loop and compare input[i] with input[i+1] if true break, else continue until i equal to the index of char befor the last.
16th Feb 2020, 7:21 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
0
Thank you all for your help. I really appreciate it. This code passed all test in the challenge. But does it work for all inputs?
17th Feb 2020, 6:16 PM
kevin
kevin - avatar