Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #20421

Re: what is the difference between these statement

From Dr Nick <3-nospam@temporary-address.org.uk>
Newsgroups comp.lang.c
Subject Re: what is the difference between these statement
Date 2012-05-06 15:39 +0100
Message-ID <87vck9xtkp.fsf@temporary-address.org.uk> (permalink)
References <05e9a565-918a-4058-a323-7cdaa921ab41@qg3g2000pbc.googlegroups.com>

Show all headers | View raw


ashu <ashishmourya21@gmail.com> writes:

> Dear friends, i am trying to solve one question about word counting
> and i am surprise by statement behaviors,
> actully i want to check if is a character not a space,tab or newline,
> i am not giving full code.
>
>
> 1st statement :
>  if(c=='  '|| c=='\n' || c== '\t') {then go to next character }
> else ( increase no of word}
>
> it checks if character is a space,tab or newline and i will do nothing
> with it.else i increase no of word. works fine
>
> 2nd statement :
> if(c!='  '|| c!='\n' || c!='\t')
> { increase no of word}
> i want to directly check if not a space,tab or newline but this
> statement is doing nothing , why?
>
> example : this is a test
> statement 1st works fine returns 2
> but statement 2nd does nothing.

The second statement should do something - because it should always be
true.  Think about it - if c is a space it isn't an newline or a tab,
and if it's a newline it isn't a space or a tab, and if it's an 'x' it
isn't a space, or a newline, or a tab.  You need && in there - you need
all the conditions to be true, not just one of them.

There is a useful function (in ctypes.h) for this - called "isspace".
So you can replace your pieces of code with:
#include <ctypes.h>
...
if(isspace((unsigned char)c) {then go to next character }
else ( increase no of word}

and
if(!isspace((unsigned char)c)
{ increase no of word}

The "(unsigned char)" bits are in for technical reasons; they prevent
the isspace function doing something silly if you pass an unusual value
to them (like and end of file, or perhaps certain accented characters
depending on various other things).  They make it slightly less elegant,
but it's still tidier and will be familiar to experienced programmers.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

what is the difference between these statement ashu <ashishmourya21@gmail.com> - 2012-05-06 04:11 -0700
  Re: what is the difference between these statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-06 12:29 +0100
    Re: what is the difference between these statement ashu <ashishmourya21@gmail.com> - 2012-05-06 07:56 -0700
    Re: what is the difference between these statement ashu <ashishmourya21@gmail.com> - 2012-05-06 08:01 -0700
      Re: what is the difference between these statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-06 18:54 +0100
  Re: what is the difference between these statement Dr Nick <3-nospam@temporary-address.org.uk> - 2012-05-06 15:39 +0100
    Re: what is the difference between these statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-06 18:32 +0100
      Re: what is the difference between these statement Dr Nick <3-nospam@temporary-address.org.uk> - 2012-05-06 19:23 +0100
        Re: what is the difference between these statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-06 22:52 +0100
          Re: what is the difference between these statement Dr Nick <3-nospam@temporary-address.org.uk> - 2012-05-07 08:08 +0100
            Re: what is the difference between these statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-07 12:44 +0100
              Re: what is the difference between these statement Dr Nick <3-nospam@temporary-address.org.uk> - 2012-05-09 06:54 +0100
                Re: what is the difference between these statement James Kuyper <jameskuyper@verizon.net> - 2012-05-09 07:46 -0400
                Re: what is the difference between these statement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-09 15:32 +0100
                Re: what is the difference between these statement Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-09 12:02 -0700
                Re: what is the difference between these statement jgk@panix.com (Joe keane) - 2012-05-10 17:46 +0000
                Re: what is the difference between these statement Keith Thompson <kst-u@mib.org> - 2012-05-10 11:47 -0700
                Re: what is the difference between these statement Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-11 08:55 -0700
                Re: what is the difference between these statement jgk@panix.com (Joe keane) - 2012-05-13 20:09 +0000
                Re: what is the difference between these statement Kaz Kylheku <kaz@kylheku.com> - 2012-05-13 23:24 +0000
                Re: what is the difference between these statement Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-14 11:46 -0700

csiph-web