Jump to: navigation, search

The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 1.12 on page 21

Write a program that prints its input one word per line.



Solution by Vidhan Gupta

/* Write a program that prints its input one word per line. */

#include <stdio.h>

#define IN 1
#define OUT 0

int main()
{

    int c, state;
    state = OUT;

    while ((c = getchar()) != EOF)
    {
        if (c == ' ' || c == '\t' || c == '\n')
            state = OUT;
        else if(state == OUT){
            state = IN;
            putchar('\n');
        }
        putchar(c);
    }

    return 0;
}
OUTPUT:
What the hell   is this

What 
the 
hell 
is 
this

Solution by Richard Heathfield (corrected by FF)

#include <stdio.h>

int main(void)
{
    int c;
    int inspace;

    inspace = 1;
    while ((c = getchar()) != EOF) {
        if (c == ' ' || c == '\t' || c == '\n') {
            if (inspace == 0) {
                inspace = 1;
                putchar('\n');
            }
            /* else, don't print anything */
        } else {
            inspace = 0;
            putchar(c);
        }
    }

    return 0;
}

Solution by Dabeau

#include <stdio.h>

#define IN	1 /* inside a word */
#define OUT	0 /* outside a word */

main()
{
	char c ;
	short state ;

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


Solution by Martijn Vruggink

#include <stdio.h>

// print input one word per line
int main()
{
    int c, prev;

    while ((c = getchar()) != EOF) {
        if (c == ' ' || c == '\n' || c == '\t')
        {   if(prev != ' ' && prev != '\n' && prev != '\t') //insert a newline if c is a "non-word" character, prev is a "word" character
	    {
               putchar('\n');
    	    }
        }
        else
        {
               putchar(c);
        }

        prev = c;
    }

    return 0;
}

Solution by Chrismath

//ex1-12 
#include <stdio.h> 

#define IN 1 
#define OUT 0 

// print input one word per line 
int main(void) 
{ 
        int c, state; 
        // start without a word 
        state = OUT; 
         
        while ((c = getchar()) != EOF) { 
                // if the char is not blank, tab, newline 
                if (c != ' ' && c != '\t' && c != '\n') { 
                        // inside a word 
                        state = IN; 
                        putchar(c); 
                // otherwise char is blank, tab, newline, word ended 
                } else if (state == IN) { 
                        state = OUT; 
                        putchar('\n'); 
                } 
        } 
        return 0;
}

Solution by Timrobinson

Timrobinson's solution [Warning: This is buggy and fails the "one word per line" part of the spec. Run it on its own source - Do'h! This "solution" should be removed. There is a reason that received solutions track "inside word" or "in space" state. Failure to grok why that is necessary is why this code fails. (unsigned comment by Nick)]

//ex1-12 
#include <stdio.h> 

// print input one word per line. Note: no need to track whether "in" or "out" of word.
int main(void) 
{ 
        int c; 
         
        while ((c = getchar()) != EOF) { 
                // if the char is not blank, tab, newline 
                if (c != ' ' && c != '\t' && c != '\n') 
                        putchar(c); 
                // otherwise char is blank, tab, newline, word ended 
				else
					putchar('\n');
		}
         return 0;
}

Solution by Tomi

Another solution:

#include <stdio.h>

 int main (void)
{
	int c,c_previous;

	while ((c=getchar()) != EOF)
	{
		if (!( (c_previous == ' ' || c_previous == '\t' || c_previous == '\n') && (c == ' ' || c=='\t' || c == '\n')))
		{
			if (c==' ' || c=='\t')  { putchar('\n'); }

			else putchar(c);
		}

		c_previous = c;
	}

	return 0;
 }

Solution by akw

#include <stdio.h>

main () {
	int c;

	while((c = getchar()) != EOF) {
		if (c != ' ' && c != '\n' && c != '\t') {
			while (c != ' ' && c != '\n' && c != '\t' && c != EOF) {
				putchar(c);
				c = getchar();
			}
			putchar('\n');
		}
	}
}

Solution by KreativitetNO

An alternative solution that uses arrays which are discussed in the next section after the exercise:

#include <stdio.h>

main() {
    int c, bp;
    char buffer[1024];
    
    bp = 0;
    while ((c = getchar()) != EOF) {
        /* any word separating character you define goes here */
        if (c != ' ' && c != '\t' && c != '\n') {
           buffer[bp++] = c;
        else if (bp > 0) {
            printf("%s\n", buffer);
            bp = 0;
        }
        buffer[bp] = '\0';
    }
    if (bp > 0)
        printf("%s\n", buffer);
}

Solution by Indianlamp

Here is my solution (sorry but this solution is utterlly wrong!!! Have you tested it?Try enter spaces or tabs first [unsigned comment added by Takumi08]):

int main()
{
	int c;
        
        while ((c = getchar()) != EOF){
		
              putchar(c);             

	      if (c == ' ' || c == '\n' || c == '\t'){
                 printf("\n");       /* or putchar('\n'); */
		}
        }

        return 0;
}

Solution by Lvictor

How about this:

#include <stdio.h>

int main(){

  int c,d;
  d = 0;

  while((c = getchar()) != EOF){
    if(c != ' ' && c != '\n' && c != '\t')
      d = 0;
      if(d == 0)
        putchar(c);
    if(c == ' '|| c == '\t'){
      d = d + 1;
      if(d == 1){
        putchar('\n');
      }
    }
  }
}

Solution by Alban Kurti

#include <stdio.h>
#include <stdlib.h>

#define OUT 0
#define IN 1

int main()
{

    int c, state;

    state = OUT;


    while ( ( c = getchar() ) != EOF){

        if ( state == IN && c == ' ' || state == IN && c == '\t' || state == IN && c == '\n' ){
            state = OUT;
            printf("\n");}

        if ( c != ' ' && c != '\t' && c != '\n' ){
            state = OUT;}

        if ( state == OUT && c != ' ' && c != '\t' && c != '\n' ){
            putchar(c);
            state = IN;}

    }

return 0;}

Solution by Takumi08

#include <stdio.h>

/*write a program that prints its input one word per line*/

int main(void)
{
	int c,charPrev;
	c=charPrev=0;

	while((c=getchar())!=EOF)
	{
		if(charPrev==0) 		
		{
			if(c!=' '&&c!='\t'&&c!='\n')	
			{
				putchar(c);
				charPrev=1;
			}
		}
		else 
		{
			if(c!=' '&&c!='\t'&&c!='\n')	
				putchar(c);
			else				
			{
				putchar('\n');
				charPrev=0;	
			}
		}
	}
return 0;
}

Solution by sl4y3r 0wn3r

/* *
 * Exercise 1-12. Write a program that prints its input one word per line.
 * */

#include <stdio.h>
#define IS_UPPER(N) ((N) >= 'A' && (N) <= 'Z')   /* 'A'==65 && 'Z'==90 */
#define IS_LOWER(N) ((N) >= 'a' && (N) <= 'z')   /* 'a'==97 && 'z'==122 */
#define IS_ALPHA(N) (IS_LOWER(N) || IS_UPPER(N)) /* [A-Za-z] x*/
#define OUT 0
#define IN  1
int main(void)
{
  int c = EOF, state = OUT;
  while ((c = getchar()) != EOF) {
    if (IS_ALPHA(c)) {
      state = IN;
      putchar(c);
    }
    else if (state == IN) {
      state = OUT;
      putchar('\n');
    }
  }

  return 0;
}

Solution by Dylan Falconer

#include <stdio.h>

#define IN  1
#define OUT 0

int main(void) {
	int c, state;

	state = OUT;
	while ((c = getchar()) != EOF) {
		if (c == '\n' || c == ' ' || c == '\t') {
			if (state == IN) {
				state = OUT;
				putchar('\n');
			}
			continue;
		}
		state = IN;
		putchar(c);
	}
}
Personal tools