[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.c

Is there a shorter solution ?

amardeep.developer

8/6/2011 5:25:00 PM

Count the number of a in cX below

#include <stdio.h>

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

Ike Naar

8/6/2011 5:43:00 PM

0

On 2011-08-06, HumbleWorker <amardeep.developer@gmail.com> wrote:
> Count the number of a in cX below
>
> #include <stdio.h>
>
> int main()
> {
> char cX[] = "ababcabcdabcaba", * k = cX;
> int numA = 0;
>
> while (*k && ('a' == *k++ ? ++numA : 1));
>
> printf ("Number of a's = %u\n", numA);

The ``%u'' format expects an unsigned int, you're supplying a signed int.
Don't do that.
Either use ``int numA'' with ``%d'', or ``unsigned int numA'' with ``%u''.

>
> return 0;
> }


--
ike@sdf.lonestar.org
SDF Public Access UNIX System - http://sdf.lo...

John Gordon

8/6/2011 5:44:00 PM

0

In <f5a3d4b3-e101-44c1-921d-6c546b1e2f78@d7g2000vbv.googlegroups.com> HumbleWorker <amardeep.developer@gmail.com> writes:

> Count the number of a in cX below

Did you really need to post this again after just five minutes?

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

Kenneth Brody

8/8/2011 1:48:00 PM

0

On 8/6/2011 1:44 PM, John Gordon wrote:
> In<f5a3d4b3-e101-44c1-921d-6c546b1e2f78@d7g2000vbv.googlegroups.com> HumbleWorker<amardeep.developer@gmail.com> writes:
>
>> Count the number of a in cX below
>
> Did you really need to post this again after just five minutes?

Because he posted from Google Groups, thinks this is a "chat room", and
figured the only reason he didn't get a reply within five minutes is because
he didn't post it correctly the first time?

--
Kenneth Brody

Keith Thompson

8/8/2011 3:09:00 PM

0

Kenneth Brody <kenbrody@spamcop.net> writes:
> On 8/6/2011 1:44 PM, John Gordon wrote:
>> In<f5a3d4b3-e101-44c1-921d-6c546b1e2f78@d7g2000vbv.googlegroups.com>
>> HumbleWorker<amardeep.developer@gmail.com> writes:
>>
>>> Count the number of a in cX below
>>
>> Did you really need to post this again after just five minutes?
>
> Because he posted from Google Groups, thinks this is a "chat
> room", and figured the only reason he didn't get a reply within
> five minutes is because he didn't post it correctly the first time?

And apparently the Google Groups Usenet interface has been having
problems. See the recent announcements in news.announce.important.
(But that doesn't particularly explain two posts in five minutes.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

amardeep.developer

8/10/2011 2:21:00 PM

0

On Aug 8, 8:09 pm, Keith Thompson <ks...@mib.org> wrote:
> Kenneth Brody <kenbr...@spamcop.net> writes:
> > On 8/6/2011 1:44 PM, John Gordon wrote:
>And apparently the Google Groups Usenet interface has been having
>problems. See the recent announcements in news.announce.important.

Yes this was the reason. I could'nt see the listing immediately, so
thought something was wrong at my end.

By the way, why are people worring about two posts ? Have we stopped
discussing C here ?

Tom St Denis

8/10/2011 4:43:00 PM

0

On Aug 6, 1:25 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote:
> Count the number of a in cX below
>
> #include <stdio.h>
>
> 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;
> }

Um shorter eh ...

int numA;
int main()
{
char *k = "abababababawhatever";
while(*k) numA += *k++ == 'a';
printf("Number of a's = %d\n", numA);
return 0;
}

:-)

amardeep.developer

8/10/2011 4:51:00 PM

0

On Aug 10, 9:43 pm, tom st denis <t...@iahu.ca> wrote:
> On Aug 6, 1:25 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote:
>
> int main()
> {
>    char *k = "abababababawhatever";
>    while(*k) numA += *k++ == 'a';
>    printf("Number of a's = %d\n", numA);
>    return 0;
>
> }
>
> :-)

Thanks ! Is it guaranteed that a logical true returns 1 ?

James Kuyper

8/10/2011 4:57:00 PM

0

On 08/10/2011 12:51 PM, HumbleWorker wrote:
> On Aug 10, 9:43 pm, tom st denis <t...@iahu.ca> wrote:
>> On Aug 6, 1:25 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote:
>>
>> int main()
>> {
>> char *k = "abababababawhatever";
>> while(*k) numA += *k++ == 'a';
>> printf("Number of a's = %d\n", numA);
>> return 0;
>>
>> }
>>
>> :-)
>
> Thanks ! Is it guaranteed that a logical true returns 1 ?

All logical expressions in C indicate truth by having a result of 1.

amardeep.developer

8/10/2011 5:14:00 PM

0

On Aug 10, 9:57 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> On 08/10/2011 12:51 PM, HumbleWorker wrote:
>
> > On Aug 10, 9:43 pm, tom st denis <t...@iahu.ca> wrote:
> >> On Aug 6, 1:25 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote:
>
> >> int main()
> >> {
> >>    char *k = "abababababawhatever";
> >>    while(*k) numA += *k++ == 'a';
> >>    printf("Number of a's = %d\n", numA);
> >>    return 0;
>
> >> }
>
> >> :-)
>
> > Thanks ! Is it guaranteed that a logical true returns 1 ?
>
> All logical expressions in C indicate truth by having a result of 1.

As we have know that any non-zero int evaluates to logical true, so
vice-versa doesn't a compiler have the freedom to return anything non-
zero ? Is there some standard that enforces that it should be 1 only ?

blp

8/10/2011 5:18:00 PM

0

HumbleWorker <amardeep.developer@gmail.com> writes:

> On Aug 10, 9:57 pm, James Kuyper <jameskuy...@verizon.net> wrote:
>> On 08/10/2011 12:51 PM, HumbleWorker wrote:
>>
>> > On Aug 10, 9:43 pm, tom st denis <t...@iahu.ca> wrote:
>> >> On Aug 6, 1:25 pm, HumbleWorker <amardeep.develo...@gmail.com> wrote:
>>
>> >> int main()
>> >> {
>> >>    char *k = "abababababawhatever";
>> >>    while(*k) numA += *k++ == 'a';
>> >>    printf("Number of a's = %d\n", numA);
>> >>    return 0;
>>
>> >> }
>>
>> >> :-)
>>
>> > Thanks ! Is it guaranteed that a logical true returns 1 ?
>>
>> All logical expressions in C indicate truth by having a result of 1.
>
> As we have know that any non-zero int evaluates to logical true, so
> vice-versa doesn't a compiler have the freedom to return anything non-
> zero ? Is there some standard that enforces that it should be 1 only ?

Yes, the C standard says so, e.g.:

6.5.13 Logical AND operator

The && operator shall yield 1 if both of its operands
compare unequal to 0; otherwise, it yields 0. The result has
type int.

....

6.5.14 Logical OR operator

The || operator shall yield 1 if either of its operands
compare unequal to 0; otherwise, it yields 0. The result has
type int.

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}