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. | Sololearn: Learn to code for FREE!

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

8/12/2022 4:52:21 PM

luis gonzalez

12 Answers

New Answer

+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..

+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?

+2

Thanks, it helped

+2

luis gonzalez welcome

+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"..

+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.

+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?

+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...

+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.

+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..

+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

+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..