# K & R C Chapter 1 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 1 of the book for easy reference.

Chapter 1: A Tutorial Introduction

1. 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.
Solution to Exercise 1-1.
2. Exercise 1-2. Experiment to find what happens when printf’s argument string contains c, where c is some character not listed above.
Solution to Exercise 1-2.
3. Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
Solution to Exercise 1-3.
4. Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
Solution to Exercise 1-4.
5. Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
Solution to Exercise 1-5.
6. Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
Solution to Exercise 1-6.
7. Exercise 1-7. Write a program to print the value of EOF.
Solution to  Exercise 1-7.
8. Exercise 1-8.Write a program to count blanks, tabs, and newlines.
Solution to  Exercise 1-8.
9. 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.
Solution to  Exercise 1-9.
10. 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.
Solution to  Exercise 1-10.
11. Exercise 1-11.How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?
Solution to  Exercise 1-11.
12. Exercise 1-12.Write a program that prints its input one word per line.
Solution to  Exercise 1-12.
13. Exercise 1-13.Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
Solution to  Exercise 1-13.
14. Exercise 1-14.Write a program to print a histogram of the frequencies of different characters in its input.
Solution to  Exercise 1-14.
15. Exercise 1-15.Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
Solution to  Exercise 1-15.
16. Exercise 1-16.Revise the the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.
Solution to  Exercise 1-16.
17. Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
Solution to  Exercise 1-17.
18. Exercise 1-18. Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.
Solution to  Exercise 1-18.
19. Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write program that reverse thes its input a line at a time.
Solution to  Exercise 1-19.
20. Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?
Solution to  Exercise 1-20.
21. Exercise 1-21. Write a program entab that replaces strings of blanks with the minimum number of tabs and blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?
Solution to  Exercise 1-21.
22. Exercise 1-22. Write a program to “fold” long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
Solution to  Exercise 1-22.
23. Exercise 1-23. Write a program to remove all comments from a C program. Don’t forget to handle quoted strings and character constants properly. C comments do not nest.
Solution to  Exercise 1-23.
24. Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don’t forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.)
Solution to  Exercise 1-24.
You can purchase the book from here or here.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

To browse more C Programs visit this link

(c) www.c-program-example.com

# K & R C Programs Exercise 1-24.

K and R C, Solution to Exercise 1-24:
C program for rudimentary syntax errors like unbalanced parentheses, brackets,quotes,and braces. This program is hard if we do it in full generality. K and R C Program Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>#define MAXLINE 1000 /* max input line size */char line[MAXLINE]; /*current input line*/int getline(void); /* taken from the KnR book. */int main() { int len = 0; int t = 0; int brace = 0, parenthesis = 0, brack = 0; int s_quote = 1, d_quote = 1; while ((len = getline()) > 0) {  t = 0;  while (t < len) {   if (line[t] == '{') {    brace++;   }   if (line[t] == '}') {    brace--;   }   if (line[t] == '[') {    brack++;   }   if (line[t] == ']') {    brack--;   }   if (line[t] == '(') {    parenthesis++;   }   if (line[t] == ')') {    parenthesis--;   }   if (line[t] == ''') {    s_quote *= -1;   }   if (line[t] == '"') {    d_quote *= -1;   }   t++;  } } if (d_quote != 1)  printf("Mismatching double quote markn"); if (s_quote != 1)  printf("Mismatching single quote markn"); if (parenthesis != 0)  printf("Mismatching parenthesisn"); if (brace != 0)  printf("Mismatching bracesn"); if (brack != 0)  printf("Mismatching bracket markn"); if (brack == 0 && brace == 0 && parenthesis == 0 && s_quote == 1 && d_quote   == 1)  printf("Syntax appears to be correct.n"); return 0;}/* getline function */int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c = getchar()) != EOF && c != 'n'; ++i)  line[i] = c; if (c == 'n') {  line[i] = c;  ++i; } line[i] = ''; return i;}`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# K & R C Programs Exercise 1-23.

K and R C, Solution to Exercise 1-23: C program that removes the all comments from a C Program. K and R C Program. Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include "stdio.h"void rcomment(int c);void in_comment(void);void echo_quote(int c);main();{ int c ,d; while((c= getchar()) !=EOF)  rcomment(c); return 0;}void rcomment(int c) { int d; if(c == '/')  if((d= getchar))=='*')  in_comment();  else if(d== '/') {   putchar(c);   rcomment(d);  } else {   putchar(c);   putchar(d);  }  else if(c=='' || c=='"')    echo_quote(c);}void in_comment() { int c, d; c = getchar(); d = getchar(); while (c!= '*' || d!!='/') {  c = d;  d = getchar(); }}void echo_quote(int c) { int d; putchar(c); while ((d = getchar()) != c) {  putchar(d);  if (d == '\')   putchar(getchar()); } putchar(d);}`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above. To get regular updates on new C programs, you can Follow @c_program
You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link

(c) www.c-program-example.com

# K & R C Programs Exercise 1-21.

K and R C, Solution to Exercise 1-21:
C Program to replace string of blanks by the minimum number of tabs and blanks to achieve the same spacing. K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include "stdio.h"#define TABSIZE 6   /*Tab increment size*/main() { int c, no_blanks, pos, no_tabs; no_blanks = 0; /*number of blamks required*/ no_tabs = 0; /* number of tabs required*/ for (pos = 1; (c = getchar()) != EOF; ++pos) {  if (c == ' ') {   if (pos % TABSIZE != 0)    ++no_blanks;   else {    no_blanks = 0;    ++no_tabs;   }  } else {   for (; no_tabs > 0; --no_tabs)    putchar('t');   if (c == 't')    no_blanks = 0;   else    for (; no_blanks > 0; --no_blanks)     putchar(' ');   putchar(c);   if (c == 'n')    pos = 0;   else if (c == 't')    pos = pos + (TABSIZE - (pos - 1) % TABSIZE);  } }}`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

To browse more C Programs visit this link
(c) www.c-program-example.com

# K&R C Program Exercise 1-12

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. C Program that prints its input one word per line.Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>int main(void){ int ch; int flag; flag = 0; while((ch = getchar()) != EOF) {  if(ch == ' ' || ch == 't' || ch == 'n')  {   if(flag == 0)   {    flag = 1;    putchar('n');   }  }  else  {   flag = 0;   putchar(ch);  } } return 0;}`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

(c) www.c-program-example.com

# K&R C Program Exercise 1-11

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?. Read more about C Programming Language .

`To test the word count program try some of the following inputs: 1. input file contains zero words , output should be 0, 0, 0.2. input file contains 1 enormous word without any newlines 3. input file contains all white space without newlines 4. input file contains 66000 newlines 5. input file contains word{huge sequence of white space of different kinds}/word 6. input file contains 66000 single letter words, 66 to the line 7. input file contains 66000 words without any newlines Some boundary conditions are:-no input-no words--just new lines-no words--just blanks,tabs, and new lines-one word per line--no blanks and tabs-word starting at the beginning of the line-word starting after some blanks.`

`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

(c) www.c-program-example.com

# K&R C Program Exercise 1-10

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. C Program to Replace each tabs by ‘t’, backspace by ‘b’, and each bakslash by ‘\’.Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>#define BACK_SLASH '\'int main(void){ int c; while((c = getchar()) != EOF) {  switch(c)  {  case 'b':   putchar(BACK_SLASH);   putchar('b');   break;  case 't':   putchar(BACK_SLASH);   putchar('t');   break;  case BACK_SLASH:   putchar(BACK_SLASH);   putchar(BACK_SLASH);   break;  default:   putchar(c);   break;  } } return 0;}`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

(c) www.c-program-example.com

# K&R C Program Exercise 1-09

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. C Program to replacing each string of one or more blanks by a single blank.Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>int main(void){ int ch; while ((ch = getchar()) != EOF) {  if (ch == ' ') {   putchar(ch);   while((ch = getchar()) == ' ' && ch != EOF)    ;  }  if (ch == EOF)   break;   putchar(ch); } return 0;}`
`Read more Similar C ProgramsC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

(c) www.c-program-example.com

# K&R C Program Exercise 1-08

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. C Program to count blanks, tabs, and newlines. Read more about C Programming Language .

` /************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>int main(void){ int bl, tb, nl; int c; int done = 0; int lastc = 0; bl = 0; tb = 0; nl = 0; while(done == 0) {  c = getchar();  if(c == ' ')   ++bl;  if(c == 't')   ++tb;  if(c == 'n')   ++nl;  if(c == EOF)  {   if(lastc != 'n')   {    ++nl;    }   done = 1;  }  lastc = c; } printf("Blanks: %dnTabs: %dnLines: %dn", bl, tb, nl); return 0;}`
`Read more Similar C ProgramsC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

(c) www.c-program-example.com

# K&R C Programs Exercise 1-05

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. C program to print the Temperature table in reverse order, that is, from 300 degrees to 0 using for loop. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact i[email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>/* print Fahrenheit-Celsius table */intmain(){ int fahr; for (fahr = 300; fahr >= 0; fahr = fahr - 20)  printf("%3d %6.1fn", fahr, (5.0/9.0)*(fahr-32)); return 0;}`
`Read more Similar C ProgramsNumber SystemK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.