WA in LAPIN

https://www.codechef.com/viewsolution/31431488
I am getting wrong answer when I am using gets() function instead of scanf().But on codeblocks it it showing the correct answer.Why is it so?How can we decide when to use scanf() and gets()?

First of all man, don’t use gets() function ever in your life again, why? because gets() introduces a nasty vulnerability in your code known as Buffer Overflow, because gets() reads everything which is present in the buffer and paste it to the memory location allocated to a string. Now a string which you have created in your program always has a fixed length but you can’t be certain that gets() will take care of that string length because it doesn’t.
You can read about Buffer Overflow Vulnerability, which you should especially if you want to write secure C programs.

NOTE:
Reference: http://www.cplusplus.com/reference/cstdio/gets/

The most recent revision of the C standard (2011) has definitively removed gets() function from its specification.
The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3).

Don’t use Code-Blocks, it a very old IDE which is not being updated for a long time, no wonder why it still supports gets().
Use Visual Studio, Geany, Notepad++, Vim, Emacs, etc, there are a lot of text editors out there that provide support for C and C++.

Hope this answers your problem.

Moreover, there is a subtle bug in the logic which you have used to solve the problem, read the problem statement carefully.
Instead of gets() use fgets(), but for this problem you can use scanf() for reading the string as there is no white-spaces present.

3 Likes

Thanks.Now I have updated my code.Here is the link.But still I am getting wrong answer.Please help me to rectify what is wrong?Here is the link to my updated solution
https://www.codechef.com/viewsolution/31487264

you are printing Yes instead of YES and No instead of NO

1 Like

Got it. Now showing the correct answer. Thank u.

Hii I tried implimenting using similar logic but getting WA.
Please help CodeChef: Practical coding for everyone

Your logic is wrong and what you have implemented is completely different from the logic you are referring to.

Consider the test-case:
1
aedb

Expected Output should be:
NO

Your Code Output:
YES
2 Likes

You are right. thnkx