Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Any solution shorter than this ? (http://www.velocityreviews.com/forums/t752526-any-solution-shorter-than-this.html)

HumbleWorker 08-06-2011 05:19 PM

Any solution shorter than this ?
 
COUNT THE NUMBER OF a IN the sting cX below

PROPOSED SOLUTION IS ->
int main()
{
char cX[] = "ababcabcdabcaba", * k = cX;
int numA = 0;

while (*k && ('a' == *k++ ? ++numA : 1));

printf ("Number of a's = %u\n", numA);

return 0;
}

John Gordon 08-06-2011 05:35 PM

Re: Any solution shorter than this ?
 
In <6fac08d8-a9b7-4057-b48e-776b7853f25a@s18g2000prc.googlegroups.com> HumbleWorker <amardeep.developer@gmail.com> writes:

> COUNT THE NUMBER OF a IN the sting cX below


> PROPOSED SOLUTION IS ->
> int main()
> {
> char cX[] = "ababcabcdabcaba", * k = cX;
> int numA = 0;


> while (*k && ('a' == *k++ ? ++numA : 1));


> printf ("Number of a's = %u\n", numA);


> return 0;
> }


I can't think of a significantly shorter solution, aside from
shenanigans like shortening variable names.

However, from a code clarity viewpoint, the while loop seems gratuitously
complex. This is easier to understand:

for(k = cX; *k; k++)
if (*k == 'a')
numA++;

And of course you haven't included the appropriate headers such as
stdlib.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


Ike Naar 08-06-2011 05:35 PM

Re: Any solution shorter than this ?
 
On 2011-08-06, HumbleWorker <amardeep.developer@gmail.com> wrote:
> COUNT THE NUMBER OF a IN the sting cX below
>
> PROPOSED SOLUTION IS ->
> int main()


int main(void)

> {
> char cX[] = "ababcabcdabcaba", * k = cX;
> int numA = 0;
>
> while (*k && ('a' == *k++ ? ++numA : 1));


while (*k) if ('a' == *k++) ++numA;

>
> printf ("Number of a's = %u\n", numA);
>
> return 0;
> }


Eric Sosman 08-06-2011 07:02 PM

Re: Any solution shorter than this ?
 
On 8/6/2011 1:35 PM, John Gordon wrote:
> In<6fac08d8-a9b7-4057-b48e-776b7853f25a@s18g2000prc.googlegroups.com> HumbleWorker<amardeep.developer@gmail.com> writes:
>
>> COUNT THE NUMBER OF a IN the sting cX below

>
>> PROPOSED SOLUTION IS ->
>> int main()
>> {
>> char cX[] = "ababcabcdabcaba", * k = cX;
>> int numA = 0;

>
>> while (*k&& ('a' == *k++ ? ++numA : 1));

>
>> printf ("Number of a's = %u\n", numA);

>
>> return 0;
>> }

>
> I can't think of a significantly shorter solution, aside from
> shenanigans like shortening variable names.


Shortest I can think of is

#include<stdio.h>
int main(){puts("Number of a's = 6");}

.... but that might fall afoul of the "Letter, not spirit" clause.

> However, from a code clarity viewpoint, the while loop seems gratuitously
> complex. This is easier to understand:
>
> for(k = cX; *k; k++)
> if (*k == 'a')
> numA++;


Or even

numA += *k == 'a';

> And of course you haven't included the appropriate headers such as
> stdlib.


He's missing <stdio.h>, but why would he need <stdlib.h>?

--
Eric Sosman
esosman@ieee-dot-org.invalid

John Gordon 08-06-2011 07:19 PM

Re: Any solution shorter than this ?
 
In <j1k356$jkr$1@dont-email.me> Eric Sosman <esosman@ieee-dot-org.invalid> writes:

> > And of course you haven't included the appropriate headers such as
> > stdlib.


> He's missing <stdio.h>, but why would he need <stdlib.h>?


I rewrote the sample program to return EXIT_SUCCESS instead of 0, and
stdlib is needed for that. But you're right, the sample program doesn't
need it.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"



All times are GMT. The time now is 07:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.