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; }
3 ответов
+ 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;
}
+ 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".
0
CarrieForle i don’t get it...