81701

What is the need of using IN and OUT state in the following script?

Question:

Hello everyone I just started learning C through THE c PROGRAMMING LANGUAGE Second Edition by Brian. W.Kernighnan (ISBN-13: 978-8131704943)

So here is a script which counts the characters, line, words

#include <stdio.h> #define IN 1 #define OUT 0 main() { int c, nl, nw, nc, state; /* c = input, nl = new line, nc = new character, nw = new word, state = (IN/OUT) */ state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf(" The number of lines is: %d,\n The number of words is: %d,\n The number of characters is: %d. \n", nl, nw, nc); }

However I made a script which does the following without the need of defining state IN and OUT

#include <stdio.h> main() { int nw, nl, nc, c ; nl = nw = nc = c = 0 ; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; else if (c == ' ' || c == '\n' || c == '\t') ++nw; } printf("Words:%d \nCharacters:%d \nLines:%d", nw, nc, nl); }

So what is the difference between these two, why does the author use the state to define IN and OUT ??

[EDIT] Oh! I see so the script is just to avoid two things : 1. To avoid word count when there are more than one spaces following the word. 2. Secondly my script would count n-2 words I suppose if proper spacing is done. Which makes the author's script more fullproof.....Is there anything else except these two ??

And thank You for your answes too....

P.S: I'm sorry this is a bit off-topic is it ok to label the question [SOLVED] or is there any other way of doing this ??

Answer1:

Your version is slightly different from his one: in your program, if you have N consecutive spaces, they will be considered as N - 1 words, because for every space you add one to the word count. Also, the last input word won't be considered.

Answer2:

IN literally means "inside a word" and OUT literally means "outside a word". He is tracking the state of the proverbial cursor as he moves through the lines.

Recommend

  • Tkinter Scrollbar not working
  • The correct way to play with data references in Perl
  • Python: Can I open two Tkinter Windows at the same time?
  • Scan text and check whether it contains word from specified list
  • Unexpected characters returned when reading email using ImapX
  • hashmap in android xml parsing
  • GCloud Functions debugging deployment failure
  • PhoneGap build + jquerymobile: onclick on button does not work
  • htaccess will not rewrite subdomains
  • Mod rewrite redirection to another domain if file not exist
  • Nginx rewrite equivalent to Apache RewriteRule that converts URL params into QueryString key/value p
  • Why cout is producing no output on Code Blocks?
  • How Get arguments value using inline assembly in C without Glibc?
  • How to make R's read_csv2() recognise the text characters properly
  • How to print columns containing value
  • Undefined references when compiling gSOAP client
  • Implementation of State Monad
  • htaccess add www if not subdomain, if subdomain remove www
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Sencha Touch 2.0 Controller refs attribute not working?
  • C++ Partial template specialization - design simplification
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • Is there a javascript serializer for JSON.Net?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • What is the “return” in scheme?
  • output of program is not same as passed argument
  • Where to put my custom functions in Wordpress?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • what is the difference between the asp.net mvc application and asp.net web application
  • Delete MySQLi record without showing the id in the URL
  • Akka Routing: Reply's send to router ends up as dead letters
  • Matrix multiplication with MKL
  • Buffer size for converting unsigned long to string
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs
  • java string with new operator and a literal