Code Monkey home page Code Monkey logo

Comments (4)

ohkimur avatar ohkimur commented on May 28, 2024

I think that my approach is a little more expandable. It can be readapted if there is the need. Of course for the purpose of the exercise your solution is more simple.

I think that generally if every text line is processed individually the approach seems more natural, at least for me.

The performance however is pretty much the same, but the logic is more abstracted.

Does this make any sense? What is your opinion?

from the-c-programming-language-2nd-edition-solutions.

aweebit avatar aweebit commented on May 28, 2024

I can't agree that my code is less expandable. For instance, it can just as easily be combined with the word counting example from the same chapter:

#include <stdio.h>

#define IN 1
#define OUT 0

main() {
    int c, nl, nw, nc, state;

    state = OUT;
    nl = nw = nc = 0;

    while ((c = getchar()) != EOF) {
        ++nc;
        if (c != ' ' && c != '\n' && c != '\t') {
            if (state == OUT) {
                ++nw;
                state = IN;
            }
            putchar(c);
        } else {
            if (c == '\n')
                ++nl;
            if (state == IN) {
                state = OUT;
                putchar('\n');
            }
        }
    }

    printf("%d %d %d\n", nl, nw, nc);
}

The newline variable is completely unnecessary. It will always be TRUE when the state is OUT, and vice versa. Always, except the very first iteration of your program, because you initialize state to OUT and newline to FALSE. And here actually comes another problem with your code. If the stream begins with a whitespace character, an unnecessary newline will be output before actual words.

from the-c-programming-language-2nd-edition-solutions.

ohkimur avatar ohkimur commented on May 28, 2024

I did some research, you are right. Can you make a pull request, but with my style of formating?

from the-c-programming-language-2nd-edition-solutions.

aweebit avatar aweebit commented on May 28, 2024

Sure. #3

from the-c-programming-language-2nd-edition-solutions.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.