Comments (4)
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.
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.
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.
Sure. #3
from the-c-programming-language-2nd-edition-solutions.
Related Issues (17)
- Your solution for Chapter 5, Exercise 1 is not correct. HOT 2
- Incomplete solution for 1.12 HOT 1
- Replace malloc with custom alloc
- Improve consistency
- Exercise 1_13 HOT 1
- Ex 1-24: Allows for closing brackets before they are opened HOT 2
- Ex 1-23: Division operator gets wrongly matched as an inline comment HOT 2
- strindex of exercise_5_06 always return -1, if the char t[] is modified to "this" HOT 1
- 1-11: A newline does not mark the end of a word
- 1.20: solution is not correct HOT 2
- [bug] 1-19 reverse.c implementation is incorrect
- exercise_4_03 does not support `-5 -5 -` HOT 1
- wrong result for 'escape sequence chain' - Exercise 1-12. HOT 3
- Fix errors on exercises that do not compile successfully HOT 1
- Too many warnings HOT 1
- Exercises page numbering
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from the-c-programming-language-2nd-edition-solutions.