[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.c

Exercise 1-8 of the C programming language

Joseph Sanoyo

8/13/2011 6:10:00 AM

Is there any way to write this code without an else if? If so how..and
can you explain it line by line please :/ I'm really new to C or
programming.

#include <stdio.h>

/* count blanks, tabs and newlines in input */
main()
{
int c, bl, t, nl;

c = 0;
bl = 0;
t = 0;
nl = 0;
while ((c = getchar()) != EOF)
if (c == ' ')
++bl;
else if (c == '\t')
++t;
else if (c == '\n')
nl;
printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
}
7 Answers

Joseph Sanoyo

8/13/2011 6:23:00 AM

0

On Aug 13, 2:10 am, Joseph Santoyo <josephsant...@gmail.com> wrote:
> Is there any way to write this code without an else if? If so how..and
> can you explain it line by line please :/ I'm really new to C or
> programming.
>
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> main()
> {
>         int c, bl, t, nl;
>
>         c = 0;
>         bl = 0;
>         t = 0;
>         nl = 0;
>         while ((c = getchar()) != EOF)
>                 if (c == ' ')
>                         ++bl;
>                 else if (c == '\t')
>                         ++t;
>                 else if (c == '\n')
>                         nl;
>         printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
>
>
>
>
>
>
>
> }

by without an else if I just meant, if statements.

io_x

8/13/2011 7:04:00 AM

0


"Joseph Santoyo" <josephsantoyo@gmail.com> ha scritto nel messaggio
news:4a72458c-aad4-4fa5-b3d5-9692eb2e470a@w18g2000yqc.googlegroups.com...
> Is there any way to write this code without an else if? If so how..and
> can you explain it line by line please :/ I'm really new to C or
> programming.
>
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> main()
> {
> int c, bl, t, nl;
>
> c = 0;
> bl = 0;
> t = 0;
> nl = 0;
> while ((c = getchar()) != EOF)
> if (c == ' ')
> ++bl;
> else if (c == '\t')
> ++t;
> else if (c == '\n')
> nl;
> printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
> }

below it is the need to add "{}" because the many if

#include <stdio.h>

/* count blanks, tabs and newlines in input */
int main(void)
{int c, bl, t, nl;

bl=0; t=0; nl=0;
while((c=getchar())!=EOF)
{if(c== ' ' ) ++bl;
if(c== '\t') ++t;
if(c== '\n') ++nl;
}
printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
}
---------------------------
or
#include <stdio.h>

/* count blanks, tabs and newlines in input */
int main(void)
{int c, bl, t, nl;

bl=0; t=0; nl=0;
while((c=getchar())!=EOF)
{bl+=(c== ' ' );
t +=(c== '\t');
nl+=(c== '\n');
}
printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
}

don't know if is garantee that if c==' ' it return 1 and not 90
it seems yes because i seen it here



Joseph Sanoyo

8/13/2011 7:28:00 AM

0

On Aug 13, 3:03 am, "io_x" <a...@b.c.invalid> wrote:
> "Joseph Santoyo" <josephsant...@gmail.com> ha scritto nel messaggionews:4a72458c-aad4-4fa5-b3d5-9692eb2e470a@w18g2000yqc.googlegroups.com...
>
>
>
>
>
>
>
>
>
> > Is there any way to write this code without an else if? If so how..and
> > can you explain it line by line please :/ I'm really new to C or
> > programming.
>
> > #include <stdio.h>
>
> > /* count blanks, tabs and newlines in input */
> > main()
> > {
> > int c, bl, t, nl;
>
> > c = 0;
> > bl = 0;
> > t = 0;
> > nl = 0;
> > while ((c = getchar()) != EOF)
> > if (c == ' ')
> > ++bl;
> > else if (c == '\t')
> > ++t;
> > else if (c == '\n')
> > nl;
> > printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
> > }
>
> below  it is the need to add "{}" because the many if
>
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> int  main(void)
> {int c, bl, t, nl;
>
>  bl=0; t=0; nl=0;
>  while((c=getchar())!=EOF)
>     {if(c== ' ' ) ++bl;
>      if(c== '\t') ++t;
>      if(c== '\n') ++nl;
>     }
>  printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);}
>
> ---------------------------
> or
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> int  main(void)
> {int c, bl, t, nl;
>
>  bl=0; t=0; nl=0;
>  while((c=getchar())!=EOF)
>     {bl+=(c== ' ' );
>      t +=(c== '\t');
>      nl+=(c== '\n');
>     }
>  printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
>
> }
>
> don't know if is garantee that if c==' ' it return 1 and not 90
> it seems yes because i seen it here

Is it just as valid to put the ++bl, ++t, ++nl below the ifs?

Ben Bacarisse

8/13/2011 1:18:00 PM

0

Joseph Santoyo <josephsantoyo@gmail.com> writes:

> On Aug 13, 2:10 am, Joseph Santoyo <josephsant...@gmail.com> wrote:
>> Is there any way to write this code without an else if? If so how..and
>> can you explain it line by line please :/ I'm really new to C or
>> programming.
>>
>> #include <stdio.h>
>>
>> /* count blanks, tabs and newlines in input */
>> main()
>> {
>>         int c, bl, t, nl;
>>
>>         c = 0;
>>         bl = 0;
>>         t = 0;
>>         nl = 0;
>>         while ((c = getchar()) != EOF)
>>                 if (c == ' ')
>>                         ++bl;
>>                 else if (c == '\t')
>>                         ++t;
>>                 else if (c == '\n')
>>                         nl;
>>         printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
>> }
>
> by without an else if I just meant, if statements.

You can do it without any selection statements at all:

while ((c = getchar() != EOF) {
bl += (c == ' ');
t += (c == '\n');
nl += (c == '\n');
}

Not saying you *should*, but then I don't understand why you want to avoid
"else if" in the first place!

--
Ben.

Tim Rentsch

9/4/2011 8:05:00 PM

0

Joseph Santoyo <josephsantoyo@gmail.com> writes:

> Is there any way to write this code without an else if? If so how..and
> can you explain it line by line please :/ I'm really new to C or
> programming.
>
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */
> main()
> {
> int c, bl, t, nl;
>
> c = 0;
> bl = 0;
> t = 0;
> nl = 0;
> while ((c = getchar()) != EOF)
> if (c == ' ')
> ++bl;
> else if (c == '\t')
> ++t;
> else if (c == '\n')
> nl;
> printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
> }

// Disclaimer: not compiled
#include <limits.h>
#include <stdio.h>

int
main( void ){
int c;
static int counts[ UCHAR_MAX ];

while( c = getchar(), c != EOF ) counts[c]++;

printf( "There are %d blanks, %d tabs and %d newlines",
counts[' '], counts['\t'], counts['\n'] );

return 0;
}

Robert Spanjaard

9/4/2011 9:08:00 PM

0

On Fri, 12 Aug 2011 23:10:06 -0700, Joseph Santoyo wrote:

> Is there any way to write this code without an else if? If so how..and
> can you explain it line by line please :/ I'm really new to C or
> programming.
>
> #include <stdio.h>
>
> /* count blanks, tabs and newlines in input */ main()
> {
> int c, bl, t, nl;
>
> c = 0;
> bl = 0;
> t = 0;
> nl = 0;
> while ((c = getchar()) != EOF)
> if (c == ' ')
> ++bl;
> else if (c == '\t')
> ++t;
> else if (c == '\n')
> nl;
> printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
> }

Just one thing that hasn't been mentioned yet: there's no need to give c
an initial value of 0.

--
Regards, Robert http://www....

David Thompson

9/9/2011 5:29:00 AM

0

On Sun, 04 Sep 2011 13:04:47 -0700, Tim Rentsch
<txr@alumni.caltech.edu> wrote:

> Joseph Santoyo <josephsantoyo@gmail.com> writes:
>
> > Is there any way to write this code without an else if? If so how..and
> > can you explain it line by line please :/ I'm really new to C or
> > programming.
> >
> > #include <stdio.h>
> >
> > /* count blanks, tabs and newlines in input */
<snip>
> int c;
> static int counts[ UCHAR_MAX ];
>
YM UCHAR_MAX+1 .

> while( c = getchar(), c != EOF ) counts[c]++;
>
> printf( "There are %d blanks, %d tabs and %d newlines",
> counts[' '], counts['\t'], counts['\n'] );
>
Nit: int (same as the OP had) could overflow. But so could long,
on many modern systems. unsigned int would make the results
well-defined though still wrong, and probably at no additional cost
(almost certainly undetectable beside the I/O cost anyway).

For completeness, another answer to the OP's literal question is
int c; /*unsigned?*/ int bl=0,t=0,nl=0, *p;
while( c=getchar(), c!=EOF ){
p = c==' '? &bl: c=='\t'? &t: c=='\n'? &nl: NULL;
if( p != NULL ) ++ *p;
}
printf /*as before or %u if unsigned*/

But really this is just 'else if' by another name.