C - The C Answer (2nd Edition) - Exercise 1-12

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

#include <stdio.h>

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

/* print input one word per line */
main()
{
	int c, state = OUT;
	while((c = getchar()) != EOF)
	{
		if(c == ' ' || c == '\n' || c == '\t')
		{
			if(state == IN)
			{
				putchar('\n');  /* finish the word */
				state = OUT;
			}
			else if(state == OUT)
			{
				state = IN;  /* beginning of word */
				putchar(c);
			}
			else  /* inside a word */
			{
				putchar(c);
			}
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 12:56:10

C - The C Answer (2nd Edition) - Exercise 1-12的相关文章

C - The C Answer (2nd Edition) - Exercise 1-18

/* Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int getline(char line[], int maxline); int remove(char s[]);

C - The C Answer (2nd Edition) - Exercise 1-19

/* Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time. */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int getline(char line[], int maxline);

C - The C Answer (2nd Edition) - Exercise 1-4

/* Write a program to print the corresponding Celsius to Fahrenheit table. */ #include <stdio.h> /* print Celsius-Fahrenheit table for celsius = 0, 20, ..., 300; floating-point version */ main() { float fahr, celsius; int lower, upper, step; lower =

C - The C Answer (2nd Edition) - Exercise 1-5

/* Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. */ #include <stdio.h> /* print Fahrenheit-Celsius table in reverse order */ main() { int fahr; for(fahr = 300; fahr >= 0; fahr -= 20

C - The C Answer (2nd Edition) - Exercise 1-7

/* Write a program to print the value of EOF. */ #include <stdio.h> main() { printf("EOF is %d\n", EOF); } /* Output: EOF is -1 */ 版权声明:本文为博主原创文章,未经博主允许不得转载.

C - The C Answer (2nd Edition) - Exercise 1-8

/* Write a program to count blanks, tabs, and newlines. */ #include <stdio.h> /* count blanks, tabs, and newlines */ main() { int c, nb, nt, nl; nb = 0; /* number of blanks */ nt = 0; /* number of tabs */ nl = 0; /* number of newlines */ while((c =

C - The C Answer (2nd Edition) - Exercise 1-9

/* Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. */ #include <stdio.h> #define NONBLANK 'a' /* replace string of blanks with a single blank */ main() { int c, lastc; lastc = NONBLANK;

C - The C Answer (2nd Edition) - Exercise 1-10

/* Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way. */ #include <stdio.h> #define NONBLANK 'a' /* replace tabs an

C - The C Answer (2nd Edition) - Exercise 1-1

/* Run the "hello, world" program on your system. Experiment with leaving out parts of the program to see what error messages you get. */ #include <stdio.h> main() { printf("hello world\n"); } 版权声明:本文为博主原创文章,未经博主允许不得转载.

C - The C Answer (2nd Edition) - Exercise 1-2

/* Experiment to find out what happens when printf's argument string contains \c, where c is some character not listed above. */ #include <stdio.h> main() { printf("hello world\y"); printf("hello world\7"); printf("hello wor