+ 2

I need help with this code for the Deja Vu task, there output says ā€˜no outputā€™ so I donā€™t know exactly whereā€™s the mistake.

#include <stdio.h> #include <string.h> int main() { char str [20]; fgets (str, 20, stdin); char first, con, ch; int i, len, t; t = 0; ch = 'n'; len = strlen (str); for (i = 0; i == len; i++){ first = str [t]; con = str [i + 1]; if (first == con){ ch = 'y'; break; } else if (i != len - 1){ i++;} else if (i == len - 1){ t++; i = t;} else if (i == len){ break;} } if (ch == 'n') printf ("Unique"); else printf ("Deja Vu"); return 0; }

12th Aug 2022, 4:52 PM
luis gonzalez
luis gonzalez - avatar
12 Answers
+ 2
#include <stdio.h> #include <string.h> int main() { char mash[128]; scanf("%s", mash); for(int i = 0; i < strlen(mash); i++) { for(int j = i + 1; j < strlen (mash); j++) { if(mash[i] == mash[j]) { printf("Deja Vu"); return 1; } } } characters printf("Unique"); return 0; } luis gonzalez You can just try this..... Hope this helps..
14th Aug 2022, 5:09 AM
ā€Ž ąŗø
+ 2
How do I break a loop whitout using the switch function? Also why is i < len false when i is brought back to 0?
12th Aug 2022, 5:42 PM
luis gonzalez
luis gonzalez - avatar
+ 2
Thanks, it helped
14th Aug 2022, 10:36 AM
luis gonzalez
luis gonzalez - avatar
+ 2
14th Aug 2022, 10:37 AM
ā€Ž ąŗø
+ 1
Input : "ABC" 'A' == 'B' false I != len-1 true I++, I++ now I=2 'A' == '\0' false I != len-1 false t!= len-1 true so t++, I=0, next i++, I=1, t=1, 'B' == 'C' false I!=len-1 true, i++ next I++, I=3 <len false. so comes out loop. No output. In first case, no output break the loop when you print once "deja vu"..
12th Aug 2022, 5:08 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
if( first == con) { printf("Deja vu") ; break; // causes loop break; } And Oh. Yes, I did not see I=0, but still same output.. I edited.. edit: oh that example is not good enough example but it is outputing more than dublicate number of time. output either "Deja vu" or "Unique" , only once.
12th Aug 2022, 6:44 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
Ok so I placed break after printf (ā€œDeja Vuā€) but it still doesnt have any output, secondly, how do I fix the ā€˜Uniqueā€™ part of the loop?
12th Aug 2022, 7:39 PM
luis gonzalez
luis gonzalez - avatar
+ 1
Take a check variable like a char or bool. char ch = 'n'; On printing "Deja vu", set it to ch='y'; At end, print result by if( ch == 'n') printf("Unique") ; // on change in ch, it won't print. In your loop, also instead of else if, use if and tak e I, not t : like if( i == len-1) printf("Unique") ; also works...
12th Aug 2022, 8:03 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
#include <stdio.h> #include <string.h> int main() { char str [20]; fgets (str, 20, stdin); char first, con, ch; int i, len, t; t = 0; ch = "n"; len = strlen (str); for (i = 0; i == len; i++){ first = str [t]; con = str [i + 1]; if (first == con){ ch = "y"; break; } else if (i != len - 1){ i++;} else if (i == len - 1){ t++; i = t;} else if (t == len - 1){ break;} } if (ch = "n") printf ("Unique"); else printf ("Deja Vu"); return 0; } // So I fixed some things that were wrong and added the ch = ā€œnā€. Now it only outputs ā€˜Uniqueā€™ regardless of the imput. Also I dont understand the alternative for ā€˜else ifā€™ that you explained.
12th Aug 2022, 11:22 PM
luis gonzalez
luis gonzalez - avatar
+ 1
ch = 'n' is assignment. Use operator == for comparison. => ch == 'n' Also note the for character, use single quotes. Using double quotes means it's a string form( "n"), not character. You have else if (t == len-1) printf("Unique") ; in loop. Instead of it, use if( i == len-1 ) printf("Unique"); So i mean it's also work..
13th Aug 2022, 10:38 AM
Jayakrishna šŸ‡®šŸ‡³
+ 1
Ok so I changed those things but the output is the same, also I replaced the t on the last else but this doesnā€™t seem to affect the output. I edited the post to show the latest code
13th Aug 2022, 11:17 AM
luis gonzalez
luis gonzalez - avatar
+ 1
Don't edit original post, next time at least. Save your update code in playground and share link here.. That helps debug problem easily.. ch == 'n'; it's comparison, return false but you are not saving result so no effect. You need ch = 'n'; here.. i=0; i==len; is false so loop won't starts now.. You need i != len; instead better to use, i < len; again ch = 'n'; initializing 'n' into ch variable. ch == 'n' is comparing ch value with 'n' character..
13th Aug 2022, 11:34 AM
Jayakrishna šŸ‡®šŸ‡³