Jump to: navigation, search

The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 2.02 on page 42

Exercise 2-2 discusses a for loop from the text. Here it is:

  for(i=0; i<lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
    s[i] = c;

Write a loop equivalent to the for loop above without using && or || .



Solution by Vidhan Gupta

/* Write a loop equivalent to the for loop above without using && or || */

#include <stdio.h>
#include <string.h>
#define MAXIMUM 1000

int getLine(char s[], int lim);

int main()
{
    int len;
    char line[MAXIMUM];
    while ((len = getLine(line, MAXIMUM)) > 0){
        printf("%d", len);
    }
    return 0;
}

int getLine(char s[], int lim)
{
    int i, j, c;
    for (i = 0, j = 0; i < lim - 1; i++, j++)
    {
        if ((c = getchar()) == EOF)
            i = lim;
        else if (c == '\n')
            i = lim;
        s[j] = c;
    }
    if (c == '\n')
    {
        s[j] = c;
        j++;
    }
    s[j] = '\0';
    return j;
}
I/O:
I'm just a badass programmer.
31

Solution by Flippant Squirrel

#include <stdio.h>

#define MAX_STRING_LENGTH 100

int main(void)
{
	/*
	for (i = 0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
		s[i] = c;
	*/

	int i = 0,
		lim = MAX_STRING_LENGTH,
		c;
	char s[MAX_STRING_LENGTH];

	while (i < (lim - 1))
	{
		c = getchar();

		if (c == EOF)
			break;
		else if (c == '\n')
			break;

		s[i++] = c;
	}

	s[i] = '\0';   /* terminate the string */

	return 0;
}


Solution by Craig Schroeder (Category 1)

Here's is my solution, which is not so much exegetic as - um - cute. :-)

#include <stdio.h>

#define lim 80

int main()
{
        int i, c;
        char s[lim];

        /* There is a sequence point after the first operand of ?: */

        for(i=0; i<lim-1 ? (c=getchar()) != '\n' ? c != EOF : 0 : 0 ; ++i)
                s[i] = c;
        
        return s[i] ^= s[i]; /* null terminate and return. */
}

Solution by Pilcrow

         i=0;
         while(i<lim-1) {
              if((c=getchar()) != '\n') {
                   if(c != EOF) {
                         s[i] = c;
                   }
              }
              i++;
         }


Solution by Valentin

My try for Category 0 solution

#include <stdio.h>
#define MAXSTRING 1001
#define END 1
#define CONTINUE 0

int check_conditions(void);

int i, lim = MAXSTRING, c;

int main(void)
{
  extern  int i, lim, c;
  char s[MAXSTRING];

  for (i = 0; check_conditions() != END; ++i)
    s[i] = c;
  s[i] = '\0';

  printf("%s\n",s);

  return 0;
}

int check_conditions(void)
{
  extern int i, lim, c;
  if (i < lim - 1)
    if ((c=getchar()) != '\n')
      if (c != EOF)
	return CONTINUE;
  return END;
}

Solution by marioloko

My try for Category 0 solution using the operands used in that chapter

include <stdio.h>
#define MAXLINE 1000 /* maximum line size */

int main(void) {
    int i, c;
    char line[MAXLINE]; /* current line */

    /* If the comparison of some of then is false then will be 0, as consequence
    * the result of the multiplication will be 0, if all are true then the
    * result will be a non zero, and the result of non-zero multiplication
    * is a non-zero element that is evalutes as true. For the || operator
    * we can use "+".
    */
    for (i = 0; (i < MAXLINE-1) * ( (c=getchar()) != '\n') * (c != EOF) ; ++i)
        line[i] = c;
    line[i] = c;
    printf("%s\n", line);
    return 0;
}

Solution by akiracadet

My try for Category 0 solution using the operands used in that chapter

#include <stdio.h>

int main(void) {
    int c, i;

    for (i = 0; ((i < 999) + ((c=getchar()) != EOF) + (c != '\n')) == 3; ++i) {
        putchar(c);
    }
}

Solution by codybartfast

My try for Category 0 solution.

enum bool { NO, YES };

i = 0;
loop = YES;
while (loop) {
	if (i >= lim - 1) {
		loop = NO;
	} else if ((c = getchar()) == '\n') {
		loop = NO;
	} else if (c == EOF) {
		loop = NO;
	} else {
		s[i] = c;
		++i;
	}
}

Solution by Rckskio

My try for Category 0 solution.

/* Exercise 2-2. 
 * Write a loop equivalent to the for loop above without using && or || . */
/* the for loop above: 
 * for (i=0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
 * 	s[i] = c; 
*/

#include <stdio.h>

#define MAXLINE	1000	/* maximum input line length */
char line[MAXLINE];	/* current line typed */

int get_line(void);

int main(void)
{
	int len;
	extern char line[];

	while ((len = get_line()) > 0)
	{
		printf("%s", line);
	}
}

int get_line(void)
{
	int c, i;
	extern char line[];

	/* The for loop without && or || */
	for (i = 0; (i < MAXLINE - 1) == ((c = getchar()) != '\n') == (c != EOF); ++i)
	{
		line[i] = c;
	}

	if (c == '\n')
	{
		line[i] = c;
		++i;
	}

	line[i] = '\0';
	
	return i;
}

Solution by Foowar

i = 0;
while (i < lim-1) {
	if (((c = getchar()) != '\n') + (c != EOF) == 2) {
		s[i] = c;
		++i;
	}
}
Personal tools