Any solution shorter than this ?

Discussion in 'C Programming' started by HumbleWorker, Aug 6, 2011.

  1. HumbleWorker

    HumbleWorker Guest

    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;
    }
     
    HumbleWorker, Aug 6, 2011
    #1
    1. Advertising

  2. HumbleWorker

    John Gordon Guest

    In <> HumbleWorker <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Aug 6, 2011
    #2
    1. Advertising

  3. HumbleWorker

    Ike Naar Guest

    On 2011-08-06, HumbleWorker <> 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;
    > }
     
    Ike Naar, Aug 6, 2011
    #3
  4. HumbleWorker

    Eric Sosman Guest

    On 8/6/2011 1:35 PM, John Gordon wrote:
    > In<> HumbleWorker<> 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
    d
     
    Eric Sosman, Aug 6, 2011
    #4
  5. HumbleWorker

    John Gordon Guest

    In <j1k356$jkr$> Eric Sosman <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Aug 6, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edwin Knoppert

    This could be shorter tight? (String to Hex)

    Edwin Knoppert, Feb 17, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    498
    Edwin Knoppert
    Feb 17, 2006
  2. Austin
    Replies:
    7
    Views:
    9,137
    Tim Tyler
    Dec 2, 2003
  3. Mercuro

    Shorter checksum than MD5

    Mercuro, Sep 9, 2004, in forum: Python
    Replies:
    17
    Views:
    1,342
    Tim Peters
    Sep 11, 2004
  4. HumbleWorker

    Is there a shorter solution ?

    HumbleWorker, Aug 6, 2011, in forum: C Programming
    Replies:
    20
    Views:
    728
    Keith Thompson
    Aug 12, 2011
  5. 2obvious

    cookies shorter than a session?

    2obvious, Aug 4, 2005, in forum: Javascript
    Replies:
    3
    Views:
    130
    Dr John Stockton
    Aug 5, 2005
Loading...

Share This Page