Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is there a shorter solution ?

Reply
Thread Tools

Is there a shorter solution ?

 
 
HumbleWorker
Guest
Posts: n/a
 
      08-06-2011
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;
}
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      08-06-2011
On 2011-08-06, HumbleWorker <(E-Mail Removed)> 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;
> }



--
http://www.velocityreviews.com/forums/(E-Mail Removed)
SDF Public Access UNIX System - http://sdf.lonestar.org
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      08-06-2011
In <(E-Mail Removed)> HumbleWorker <(E-Mail Removed)> 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
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-08-2011
Kenneth Brody <(E-Mail Removed)> writes:
> On 8/6/2011 1:44 PM, John Gordon wrote:
>> In<(E-Mail Removed)>
>> HumbleWorker<(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
HumbleWorker
Guest
Posts: n/a
 
      08-10-2011
On Aug 8, 8:09*pm, Keith Thompson <(E-Mail Removed)> wrote:
> Kenneth Brody <(E-Mail Removed)> 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 ?
 
Reply With Quote
 
tom st denis
Guest
Posts: n/a
 
      08-10-2011
On Aug 6, 1:25*pm, HumbleWorker <(E-Mail Removed)> 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;
}


 
Reply With Quote
 
HumbleWorker
Guest
Posts: n/a
 
      08-10-2011
On Aug 10, 9:43*pm, tom st denis <(E-Mail Removed)> wrote:
> On Aug 6, 1:25*pm, HumbleWorker <(E-Mail Removed)> 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 ?
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      08-10-2011
On 08/10/2011 12:51 PM, HumbleWorker wrote:
> On Aug 10, 9:43 pm, tom st denis <(E-Mail Removed)> wrote:
>> On Aug 6, 1:25 pm, HumbleWorker <(E-Mail Removed)> 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.

 
Reply With Quote
 
HumbleWorker
Guest
Posts: n/a
 
      08-10-2011
On Aug 10, 9:57*pm, James Kuyper <(E-Mail Removed)> wrote:
> On 08/10/2011 12:51 PM, HumbleWorker wrote:
>
> > On Aug 10, 9:43 pm, tom st denis <(E-Mail Removed)> wrote:
> >> On Aug 6, 1:25 pm, HumbleWorker <(E-Mail Removed)> 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 ?
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-10-2011
HumbleWorker <(E-Mail Removed)> writes:

> On Aug 10, 9:57*pm, James Kuyper <(E-Mail Removed)> wrote:
>> On 08/10/2011 12:51 PM, HumbleWorker wrote:
>>
>> > On Aug 10, 9:43 pm, tom st denis <(E-Mail Removed)> wrote:
>> >> On Aug 6, 1:25 pm, HumbleWorker <(E-Mail Removed)> 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 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Is there a shorter, more elegant way to write this deep hashlookup statement Dr.Ruud Perl Misc 1 02-21-2013 04:37 PM
Re: Is there a shorter, more elegant way to write this deep hashlookup statement C.DeRykus Perl Misc 0 02-19-2013 07:40 PM
Re: Is there a shorter, more elegant way to write this deep hash lookup statement George Mpouras Perl Misc 0 02-17-2013 12:07 PM
Any solution shorter than this ? HumbleWorker C Programming 4 08-06-2011 07:19 PM
is there a shorter way to compare these 2 objects? Lowell Kirsh Ruby 5 08-18-2005 10:56 PM



Advertisments