K&R C Program Exercise 1-17

K and R solution Exercise 1-17,to print all input lines that are longer than 80 characters. 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 MAXINLINE 1000 /* maximum line sizes defined*/
#define LONGLINESIZE 80 /* longer line defined as 80 */
int get_lines(char line[], int maxline);
/*print lines longer than LONGLINESIZE */
main()
{
int len; /*current line length */
char line[MAXINLINE];/* current input line */
while((len=get_lines(line,MAXINLINE))>0)
if(len> LONGLINE)
printf("%s", line);
return 0;

}

/* getline: read a line into s, return length */
int get_lines(char s[], int line)
{
int c, i, j;

for(i = 0, j = 0; (c = getchar())!=EOF && c != 'n'; ++i)
{
if(i < line - 1)
{
s[j++] = c;
}
}
if(c == 'n')
{
if(i <= line - 1)
{
s[j++] = c;
}
++i;
}
s[j] = '';
return i;
}
Read more Similar C Programs
C Basic

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

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

K&R C Program Exercise 1-16

K and R solutions to 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. 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 MAXINLINE 1000 /* maximum input line size */

int getline(char line[], int MAXINLINE);
void copy(char to[], char from[]);

/* print longest input line */
int main(void)
{
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXINLINE]; /* current input line */
char longest[MAXINLINE]; /* longest line saved here */

max = 0;

while((len = getline(line, MAXINLINE)) > 0)
{
printf("%d: %s", len, line);

if(len > max)
{
max = len;
copy(longest, line);
}
}
if(max > 0)
{
printf("Longest LINE IN THE PROGRAM is %d characters:n%s", max, longest);
}
printf("n");
return 0;
}

/* getline: read a line into s, return length */
int getline(char s[], int line)
{
int c, i, j;

for(i = 0, j = 0; (c = getchar())!=EOF && c != 'n'; ++i)
{
if(i < line - 1)
{
s[j++] = c;
}
}
if(c == 'n')
{
if(i <= line - 1)
{
s[j++] = c;
}
++i;
}
s[j] = '';
return i;
}

/* copy: copy 'from' into 'to'; assume 'to' is big enough */
void copy(char to[], char from[])
{
int k;

k = 0;
while((to[k] = from[k]) != '')
{
++k;
}
}


Read more Similar C Programs
C Basic

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

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

K&R C Program Exercise 1-15

Rewrite the temperature conversion program (K&R C Program Exercise 1-03 ) to use a function for conversion. 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"
float celsius(float fahr);
/* Print Fahrenheit-Celsius table
for fahr=0, 20, . . , 300; float-point version */
main()
{
float fahr;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;

fahr = lower;
while(fahr <= upper)
{

printf("%3.0f %6.1fn", fahr, celsius(fahr));
fahr = fahr + step;
}

}
/* celsius: convert fahr into celsius */
float celsius(float fahr)
{
return (5.0 / 9.0) * (fahr - 32.0);
}
Read more Similar C Programs
C Basic

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

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

K&R C Program Exercise 1-14

K and R solutions to print a histogram of the frequencies of different characters in its input. 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
#include

#define HISTLEN 15 /*max length of histogram */
#define CHARLENGTH 128 /*max different characters */

/* print horizontal histogram freq. of different characters */
main()
{
int c,i;
int len; /*length of each bar */
int maxvalue; /*maximum value for cc[] */
int cc[CHARLENGTH]; /*character counters*/

for(i=0;imaxvalue)
maxvalue=cc[i];
for(i=1;i0) {
if((len = cc[i]*HISTLEN/maxvalue)<=0)
len=1;
}
else
len=0;
while(len>0)
{
putchar(*);
--len;
}
putchar('n');
}
}
Read more Similar C Programs
C Basic

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

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

K&R C Program Exercise 1-13

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. K and R solution to print a histogram of the lengths of words in its input. 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 MAXLN 10

int main(void)
{
int c;
int inspace = 0;
long lengtharr[MAXLN + 1];
int wordlen = 0;

int firstletter = 1;
long value = 0;
long maxval = 0;
int index = 0;
int done = 0;

for(index = 0; index <= MAXLN; index++)
{
lengtharr[index] = 0;
}

while(done == 0)
{
c = getchar();

if(c == ' ' || c == 't' || c == 'n' || c == EOF)
{
if(inspace == 0)
{
firstletter = 0;
inspace = 1;

if(wordlen <= MAXLN)
{
if(wordlen > 0)
{
value = ++lengtharr[wordlen - 1];
if(value > maxval)
{
maxval = value;
}
}
}
else
{
value = ++lengtharr[MAXLN];
if(value > maxval)
{
maxval = value;
}
}
}
if(c == EOF)
{
done = 1;
}
}
else
{
if(inspace == 1 || firstletter == 1)
{
wordlen = 0;
firstletter = 0;
inspace = 0;
}
++wordlen;
}
}

for(value = maxval; value > 0; value--)
{
printf("%4d | ", value);
for(index = 0; index <= MAXLN; index++)
{
if(lengtharr[index] >= value)
{
printf("* ");
}
else
{
printf(" ");
}
}
printf("n");
}
printf(" +");
for(index = 0; index <= MAXLN; index++)
{
printf("---");
}
printf("n ");
for(index = 0; index < MAXLN; index++)
{
printf("%2d ", index + 1);
}
printf(">%dn", MAXLN);

return 0;
}
Read more Similar C Programs
C Basic

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!


List of C Programs
(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 Programs
C Basic

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

(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 Programs
C Basic

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

(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 Programs
C Basic

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

(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 Programs
C Strings

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

(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 Programs
C Strings

K 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!

Share this program with your Facebook friends now! by liking it

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

Like to get updates right inside your feed reader? Grab our feed!

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