[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Any solution shorter than this ?

amardeep.developer

8/6/2011 5:19:00 PM

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;
}
7 Answers

John Gordon

8/6/2011 5:35:00 PM

0

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

8/6/2011 5:36:00 PM

0

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;
> }

robertwessel2@yahoo.com

8/6/2011 6:01:00 PM

0

On Sat, 6 Aug 2011 17:35:43 +0000 (UTC), Ike Naar
<ike@sdf.lonestar.org> wrote:

>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;

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

(the parenthesis could obviously be removed, included here for
clarity)

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

Eric Sosman

8/6/2011 7:02:00 PM

0

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

8/6/2011 7:19:00 PM

0

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"

io_x

8/8/2011 6:53:00 AM

0


"Robert Wessel" <robertwessel2@yahoo.com> ha scritto nel messaggio
news:640r37psls9sgc4quckv569vbfq3j97a0k@4ax.com...
> On Sat, 6 Aug 2011 17:35:43 +0000 (UTC), Ike Naar
> <ike@sdf.lonestar.org> wrote:
>
>>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;
>
> while (*k) numA += ( 'a' == *k++);
for(; *k; ++k)
numA+=(*k==a);

> (the parenthesis could obviously be removed, included here for
> clarity)
>
>>
>>>
>>> printf ("Number of a's = %u\n", numA);
>>>
>>> return 0;
>>> }



io_x

8/8/2011 9:27:00 AM

0


"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4e3f872e$0$15666$4fafbaef@reader2.news.tin.it...
> for(; *k; ++k)
> numA+=(*k==a);

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