John Gordon
8/6/2011 5:35:00 PM
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"