From d6e0262ddbbbea7d207631a7889bb2e83a11d79d Mon Sep 17 00:00:00 2001 From: zlg Date: Thu, 28 Feb 2013 00:03:30 -0600 Subject: Change 2-02's solution to a while loop A recursive function is a bit much for something that simple. --- ch2/2-02_no-logical-operators.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'ch2/2-02_no-logical-operators.c') 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; } -- cgit v1.2.3-54-g00ecf