Giter Site home page Giter Site logo

ccpalettes / the-c-programming-language-second-edition-solutions Goto Github PK

View Code? Open in Web Editor NEW
564.0 47.0 168.0 52 KB

Solutions for all exercises in the book "The C Programming Language - Second Edition"(referred to as K&R, after its authors' initials) by Brian W. Kernighan and Dennis M. Ritchie.

C 100.00%

the-c-programming-language-second-edition-solutions's Introduction

the-c-programming-language-second-edition-solutions

Solutions for all exercises in the book "The C Programming Language - Second Edition"(referred to as K&R, after its authors' initials) by Brian W. Kernighan and Dennis M. Ritchie.

the-c-programming-language-second-edition-solutions's People

Contributors

ccpalettes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

the-c-programming-language-second-edition-solutions's Issues

2-4 does not terminate

I find the program does not terminate on Windows.
But the following code works:

void squeeze(char s1[], char s2[])
{
    int i=0;//s1 ptr
    char tmp[MAXLINE];
    int k=0;//tmp ptr
    while(s1[i]!='\0'){
        int find_flag = 0;
        int j=0;//s2 ptr
        while(s2[j]!='\0'){
            if(s1[i]==s2[j]){//match!
                find_flag=1;
                break;
            }
            j++;
        }
        if(find_flag==0){
            tmp[k]=s1[i];
            k++;
        }
        i++;
    }
    tmp[k]='\0';
    for(int ii=0;ii<k+1;ii++)
        s1[ii]=tmp[ii];
}

Longest-line program (1.17)

The exercise explicitly mentions that you have to revise the main routine of the program, yet the solution has revised the get_line function. https://github.com/ccpalettes/the-c-programming-language-second-edition-solutions/blob/master/Chapter1/Exercise%201-16/longest_line.c

That's why I did it like so:

main()
{
    int len;            /* current line length */
    int counter;        /* counts how many times len has reached MAXLINE - 1 */
    int max;            /* maximum length seen so far */
    char firstline[MAXLINE];/* first part of the line to be printed */
    char line[MAXLINE];     /* current input line */
    char longest[MAXLINE];  /* longest line saved here */

    counter = max = 0;
    while ((len = fetchline(line, MAXLINE)) > 0) {
        while (len == MAXLINE - 1 && line[MAXLINE - 1] != '\n') {
            if (counter == 0)
                copy(firstline, line);
            ++counter;
            len = fetchline(line, MAXLINE);
        }
        len = counter * (MAXLINE - 1) + len;
        if (len > max) {
            max = len;
            if (counter > 0)
                copy(longest, firstline);
            else
                copy(longest, line);
        }
        counter = 0;
    }
    if (max > 0)    /* there was a line */
        printf("%s\n%d\n", longest, max);
    return 0;
}

Before this exercise is presented, the authors even suggest this solution ("By testing the length and the last character returned, main can determine whether the line was to long, and then cope as it wishes."). That is why I believe this is actually the correct solution.

Excersise 4-13 broken for even length inputs.

Any input with even length will not properly reverse the middle two characters.

For example, inputting 12 will output 12, inputting 1234 will output 4231, inputting 123456 will output 653421, etc.

Odd length inputs work as intended.

exercise 1.12 does not ignore first whitespace

$ printf '  word1 word2 word3\tword4\nword5 \n word6\tword7\n' | ./test # your code

word1
word2
word3
word4
word5
word6
word7

I'm using something like this:

#include <stdio.h>

int
main()
{
        int z;

        word = 0;

        while ((z = getchar()) != EOF) {
                if (z == ' ' || z == '\t' || z == '\n') {
                        if (word) {
                                printf("\n");
                                word = 0;
                        } else
                                continue;
                } else {
                        putchar(z);
                        word = 1;
                }
        }
        return(0);
}
$ printf '  word1 word2 word3\tword4\nword5 \n word6\tword7\n' | ./main
word1
word2
word3
word4
word5
word6
word7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.