# Help me golf this C program checking for string patterns in a sorted hand of cards

I could not think of a better title.

This is a tips question. I'm asking for tips on golfing my code.

I recently took part in a code golf competition in C. The competition ended and I lost. The problem is:

Input a line with a number n.

Then input n lines, each line contains only a set of cards in descending order, e. g. X2AAKKKKQT9765433 or DX22AKKQJTT884443. Figure out if it contains DX or four of the same cards in a row.

Obviously, this is a chameleon challenge (finding DX is essentially the same as checking the second character if it is X, and to check for four of the same cards you only need to check the first and the last card of the quadruplet, because the cards are in descending order.)

In that competition, the input format is strict, and error-terminating is disallowed.

After the competition, we continued to improve on this problem. We've got this so far (98 bytes):

char*p,s[99];main(){for(gets(s);gets(p=s);puts(s[1]>87?"Yes":"No"))for(;*p;)s[1]|=*p^p++[3]?0:88;}


I'm looking for tips to golf this down further.

• I considered main function recursion to no avail.
– null
Aug 7 at 15:49
• What do the D and X stand for? I can only guess that one of them represents a joker. Aug 7 at 19:32

94 bytes

Instead of increasing s[1] when you find a quadruplet, setting it to zero is a bit cheaper. The conditional in puts stays the same length:

char*p,s[99];main(){for(gets(s);gets(p=s);puts(s[1]%88?"No":"Yes"))for(;*p;)s[1]*=*p!=p++[3];}


Try it online!

dingledooper suggested two more improvements:

93 bytes

If we can avoid to produce in the first iteration, The initial call to gets can be dropped. This is achieved by assuming the first argument to main (argc) is 1, which is the case if there are no command line arguments.

char*p,s[99];main(i){for(;gets(p=s);--i&&puts(s[1]%88?"No":"Yes"))for(;*p;)s[1]*=*p!=p++[3];}


Try it online!

87 bytes

If you stop the inner for loop as soon a quadruplet is found or the end of input is reached, *p>0 indicates a quadruplet:

char*p,s[99];main(i){for(;gets(p=s);--i&&puts(s[1]>87|*p?"Yes":"No"))for(;*p++-p[2];);}


Try it online!

• You can also just enter s[] rather than s[99]. It worked in the tests cases Aug 7 at 17:16
• @TKirishima For me, I get error: array size missing in 's'. Aug 7 at 19:06
• @TKirishima It does work on TIO, though it starts falling into the realm of undefined behavior. The program may segfault at any point since s can technically only store 1 element using that declaration. Aug 7 at 19:18
• 87 bytes: char*p,s[99];main(i){for(;gets(p=s);--i&&puts(s[1]>87|*p?"Yes":"No"))for(;*p++-p[2];);} Aug 7 at 20:52
• @ovs When initialized in the global scope, s[99] should be filled with zeros. Even if p goes past the "end", I believe there would be enough 0s afterwards to terminate the loop. Aug 8 at 22:33