Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Is there a shorter solution ? (http://www.velocityreviews.com/forums/t752528-is-there-a-shorter-solution.html)

HumbleWorker 08-06-2011 05:25 PM

Is there a shorter solution ?
 
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;
}

Ike Naar 08-06-2011 05:42 PM

Re: Is there a shorter solution ?
 
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.lonestar.org

John Gordon 08-06-2011 05:44 PM

Re: Is there a shorter solution ?
 
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"


Keith Thompson 08-08-2011 03:09 PM

Re: Is there a shorter solution ?
 
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.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

HumbleWorker 08-10-2011 02:20 PM

Re: Is there a shorter solution ?
 
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 08-10-2011 04:43 PM

Re: Is there a shorter solution ?
 
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;
}

:-)

HumbleWorker 08-10-2011 04:51 PM

Re: Is there a shorter solution ?
 
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 08-10-2011 04:57 PM

Re: Is there a shorter solution ?
 
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.


HumbleWorker 08-10-2011 05:13 PM

Re: Is there a shorter solution ?
 
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 ?

Ben Pfaff 08-10-2011 05:17 PM

Re: Is there a shorter solution ?
 
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,0xa6 7f6aaa,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;}}}


All times are GMT. The time now is 05:13 AM.

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