Kernighan and Ritchie problems solution

Contents

If you have an a.out file and don’t know what to do with it, try:

./a.out

Page 29

#include<stdio.h> #define LOWER 0 #define UPPER 300 #define STEP 20 main() /*Named constants*/ { int fahr, fahr1; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) printf("%5d %10f.1\n", fahr, 5.0/9.0*(fahr-32.0)); }

If you don’t put .0 after 32, my Avast antivirus writes about the threat.
The same is true of all previous examples where
for is used

Page 34, paragraph 1.5.3 Counting lines.

if (c == '\n')

Not some mysterious V.


Page 35, exercise 1.8. You need to write a program to count tabs, spaces and new lines.

I wrote two options. One works right.

#include<stdio.h> /*calculation of spaces, tabs and new lines of input stream*/ main() { int c, nl; nl = 0; while ((c=getchar()) !=EOF) if (c=='\n') ++nl; else if (c=='\t') ++nl; else if (c==' ') ++nl; printf("%d\n", nl); }

The other one counts everything.



#include<stdio.h>
/*calculation of spaces, tabs and new lines of input stream*/
main()
{
int c, nl;
nl = 0;
while ((c=getchar()) !=EOF)
if (c=='\n','\t',' ')
++nl;
printf("%d\n", nl);
}

Blue says that the if condition here is always TRUE.
The last character in the condition if (c=='\n','\t',' ') is considered logical by the compiler.
In ASCII, the space value is 20. 20. > 0, anything more than zero counts as TRUE, i.e. any symbol contributes to the sum.
I checked, if you put just zero there, then nothing will be displayed. Zero is FALSE.

Exercise Solutions from Book B. Kernighan and D. Ritchie.
"The C Programming Language"

These are my solutions, at 99%, so they may be quite hacky.

Found a websitewhere solutions are also posted.

1.15

#include <stdio.h> float ffahr(int c); main() { int cel,i; for (i =0;i<20; ++i){ cel = i*10; printf("%d %f\n", cel, ffahr(cel)); } return 0; } float ffahr(int c) { float fahr; fahr = (9.0/5.0)*c + 32.0; return fahr; }

1.17

#include <stdio.h> #define MAXLINE 1000 /* maximum line length in stream */ int fgetline(char line[],int maxline); void copy(char to[], char from[]); /* output the longest string in the stream */ main() { int len;/* the length of the current line */ int max;/* current maximum length */ char line[MAXLINE]; /* current maximum length */ char longest [MAXLINE]; /* the longest line entered */ char more50 [MAXLINE]; char more51 [MAXLINE]; char more52 [MAXLINE]; char more53 [MAXLINE]; char more54 [MAXLINE]; char more55 [MAXLINE]; char more56 [MAXLINE]; int j,i; j =0; max = 0; while ((len = fgetline(line, MAXLINE)) > 0 ){ /* if (len > max) { max = len; copy(longest, line); */ /*if (max > 0) /* was a non-empty line */ /*printf("%s", longest); return 0;*/ if (len >= 5 ) { copy(more5j, line); ++j; } } printf("%s", more5); } /* getline: reads a line in s, returns its length */ int fgetline(char s[], int lim) { int c,i; for (i = 0; i<lim-1 && (c=getchar()) !=EOF && c!='\n';++i) s[i] = c; if (c =='\n'){ s[i] = c; ++i; } s[i] = '\0'; return i; } /* copy: copies the line 'from' in 'to' length to is considered sufficient */ void copy(char to[], char from[]) { int i; i=0; while((to[i] = from[i]) !='\0') ++i; }

1.18

#include <stdio.h> #define MAXLINE 1000 int fgetline(char line[], int maxline); void reverse(char to[], char from[]); main() { int c,i,j,max,len,k; i=0; j=2; char line [MAXLINE]; /*while ((len = fgetline(line, MAXLINE))>1){*/ len = fgetline(line, MAXLINE); while (line[len-j] ==' ' || line[len-j]=='\t') ++j; for (k=0;k<=len-j;++k) printf("%c", line[k]); printf("%c\n", '\t'); } /*printf("%c\n", line[len-2]);*/ int fgetline(char s[], int lim) { int c, i; for (i=0; i< lim-1 && (c = getchar()) !=EOF && c !='\n';++i ) s[i] =c; if (c=='\n'){ s[i] = c; ++i; } s[i] = '\0'; return i; }

1.19

#include <stdio.h> #define MAXLINE 1000 void reverse(char to[], char from[]); main() { int c,i,j,max; i=0; char line [MAXLINE]; char revline [MAXLINE]; while ((c=getchar())!=EOF && c!='\n') { line[i] = c; ++i; } max=i; for (j=0;j<=max;++j){ revline[j]=line[i]; --i; } printf("%s\n",revline); printf("%s\n",line); return 0; }

1.20

#include <stdio.h> main() { int c,i,j; i=0; while((c=getchar())!=EOF){ if(c!='\t'&& c!='\n'){ ++i; //printf("%d",i); if(i==8){ i=0;} printf("%c",c); } if(c=='\t'){ for (j=0;j<8-i;++j){ printf("%c",' ');} i=0; } else if(c=='\n'){ printf("\n","new line"); i=0;} /*else printf("%c",c);*/ } printf("\n"); }

1.22

#include <stdio.h> #define lim 10 #define maxline 50 main() { int c,i,j,start,start2,len,len2,len3; int line[maxline]; int line2[maxline]; start=0; len3=0; for(i=0;i<maxline;++i){ line[i]=('A'); } i=0; while((c=getchar())!=EOF && i<maxline) { if(c!='\n'){ line[i]=c; ++i;} else {;} } /* commentr rii 1234 */ len=i; for(start=0;start<(maxline-9);start +=len3){ for(j=(start+9);j<(start+0);--j){ if (line[j]==' '){ len2=j; j=0; } else { len2= start + 9; }} for(i=start;i<=len2;++i) { printf("%c",line[i]); } printf("\n"); //printf("%d",start); len3=(len2-start+1); }

1.23

#include <stdio.h> #define lim 10 #define maxline 5000 main() { int c,i,i1,j,j1,j2,len; int line[maxline]; int line2[maxline]; len=0; j=j1=j2=0; while((c=getchar())!=EOF){ ++i; line[i]=c; } for(j=0;j<=i;++j){ if(line[j]=='/'){ j1=j+1; if(line[j1]=='/'){ for(j=j;j < i && line[j]!='\n';++j) ++len; } else if(line[j1]=='*'){ j2=j; for(j2=j;j2<i;++j2) { if(line[j2]!='*') { ++len; ++j; } else if(line[j2]=='*') { j1=j2+1; if(line[j1]!='/') { ++len; ++j; } else { ++j; j2=i; } } } } else { line2[j-len]=line[j]; } else line2[j-len]=line[j]; // dljkflkdjfl;kjsdlkf;klsdjfj;sdljf;klsdjfk;ljsdk;flj;sdlkjfkl;sjdfl;ksjdflk;js;dlkfj;klf9 } printf("Old"); printf("\n"); for(i1=0;i1<=i;++i1){ printf("%c",line[i1]); } printf("\n"); printf("New"); printf("\n"); for(i1=0;i1<=i;++i1){ printf("%c",line2[i1]); } printf("\n"); /* dflkdsjflkj dsjklfjkd lkjdlfjk */ return 0; } //printf("%c", line[j]); printf("\n"); return 0; }

1.24

#include<stdio.h>
#define maxline 5000
main()
{
int c,i,j,j1,j2;
int round; // check round brackets
int fig; //figure
int quad;
int line[maxline];
i=round=fig=quad=0;
while((c=getchar())!=EOF)
{
line[i]=c;
++i;
}

for(j=0;j < i;++j)
{
if(line[j]=='/'){
j1=j+1;
if(line[j1]=='/'){
for(j=j;j < i && line[j]!='\n';++j){
}
}
else if(line[j1]=='*'){
for(j2=j;j2 < i;++j2,++j){

if(line[j]=='*'){
j1=j+1;
if(line[j1]=='/'){
++j;
j2=i;
}
}
}
}
}

else if (line[j]=='('){
++round;
}
else if (line[j]==')'){
--round;
}
else if (line[j]=='{'){
++fig;
}
else if (line[j]=='}'){
--fig;
}
}

if(round == 0){
printf("round ok");
}
else if(round > 0){
printf("round mistake to many (");
}
else if(round < 0){
printf("round mistake not enought (");
}
printf("\n");
if(fig == 0){
printf("fig ok");
} else if(fig > 0){
printf("fig mistake to many {");
}
else if(fig < 0){
printf("fig mistake not enought {");
}
printf("\n");
return 0;
}

2.1

#include <stdio.h>
#include <limits.h>
#include <float.h>

When I read the assignment, it was a little bit off. How to have knowledge of the first chapter of K&R
(from which I made all the exercises myself) to understand how to access the files. Blue
He said that the headers are in the /usr/includes folder.
int main()
{

return 0;
}




I've been struggling with example 1.9 for a long time. You had to write a program that outputs what you type from the keyboard, while ignoring all the spaces, except the very first ones.

#include<stdio.h>
main()
{
int c, c1;
c1 == 0;
while ((c=getchar()) !=EOF){
if (c==' ')
++c1;
else
c1 = 0;
if (c1 <=1)
putchar(c) ;
else
{
;
c1 = 1;
}
}
}

Article author: Andrei Olegovich

Related articles
C development
Textbook on C
Boolean in C
Sockets in C
K&R
What is argc, char * argv[]
Function scanf()
Pointers
Structures in C
Operator «arrow» pointer to member structure

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: