0

need help with deja vu challenge in C

Output is only Unique. is my logic wrong? Heres my code: #include <stdio.h> #include <string.h> int main() { char str[100]; fgets(str,sizeof str,stdin); int i; int j; char l; char k; int len = strlen(str); for(i = 0;i < len;i++){ l = str[i]; for(j = i+1;j < len;j++){ k = str[j]; if(l != k){ printf("Deja Vu"); exit(1); } else{ printf("Unique"); exit(1); } } } return 0; }

17th Jan 2021, 10:21 PM
nick
nick - avatar
3 Antworten
+ 3
Try this: #include <stdio.h> #include <string.h> int main() { int flag = 1; char str[100]; fgets(str,sizeof(str),stdin); for(int i=0;i<strlen(str);i++) { for(int j=i+1;j<strlen(str);j++) { if(str[i] == str[j]) { flag = 0; break; } } } if(flag) { printf("Unique"); } else { printf("Deja Vu"); } return 0; }
18th Jan 2021, 7:29 AM
K.S.S. Karunarathne
K.S.S. Karunarathne - avatar
+ 1
You only checked str[0] which is str[i] and str[1] which is str[j]. When str[0] != str[1] it prints "deja vu", else prints "Unqiue".
17th Jan 2021, 10:51 PM
你知道規則,我也是
你知道規則,我也是 - avatar
0
CarrieForle i don’t get it...
17th Jan 2021, 11:15 PM
nick
nick - avatar