diff options
author | zlg <zlg@zlg.space> | 2013-02-28 00:03:30 -0600 |
---|---|---|
committer | zlg <zlg@zlg.space> | 2013-02-28 00:03:30 -0600 |
commit | d6e0262ddbbbea7d207631a7889bb2e83a11d79d (patch) | |
tree | d53e282f41a96a94edb00d2ba5b1a3fa755c2e06 /ch2 | |
parent | Solve Exercise 2-02: No logical operators (diff) | |
download | knr-d6e0262ddbbbea7d207631a7889bb2e83a11d79d.tar.gz knr-d6e0262ddbbbea7d207631a7889bb2e83a11d79d.tar.bz2 knr-d6e0262ddbbbea7d207631a7889bb2e83a11d79d.tar.xz knr-d6e0262ddbbbea7d207631a7889bb2e83a11d79d.zip |
Change 2-02's solution to a while loop
A recursive function is a bit much for something that simple.
Diffstat (limited to '')
-rw-r--r-- | ch2/2-02_no-logical-operators.c | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/ch2/2-02_no-logical-operators.c b/ch2/2-02_no-logical-operators.c index 6fea5d6..6fa1601 100644 --- a/ch2/2-02_no-logical-operators.c +++ b/ch2/2-02_no-logical-operators.c @@ -9,8 +9,9 @@ * * for (i=0; i < lim - 1 && (c = getchar()) != '\n' && c != EOF) * i++; + * ) * - * Answer: A recursive function should do the job. I think... + * Answer: A while loop and a few 'if's should do it. * */ @@ -20,25 +21,20 @@ char test[LIMIT] = ""; int i = 0; int c; -void save_string(char s[]) { - if (i < LIMIT - 1) { +int main() { + while (i < LIMIT - 1) { c = getchar(); if (c != '\n') { if (c != EOF) { - s[i] = c; + test[i] = c; i++; } else { - return; + break; } } else { - return; + break; } - save_string(s); } -} - -int main() { - save_string(test); printf("%s\n", test); return 0; } |