第一章16~19

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33528164/article/details/87854648

1.16

#include <stdio.h>
#define MAXLINE 1000 /*maximum input line size*/

int getlines(char line[], int maxline);
void copy(char to[], char from[]);

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

    max = 0;
    while((len = getlines(line, MAXLINE)) > 0){
        printf("%d  %s", len, line);
        if(len > max){
            max = len;
            copy(longest, line);
        }
    }
    if(max > 0){                /* there was a line*/
        printf("%s", longest);
    }
    return 0;
}

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

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

/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[]){
    int i = 0;
    while((to[i] = from[i])!='\0'){
        ++i;
    }
}

1.17

/* 第一种解法 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

#define EIGHTY 80

int main(){
    char *line = NULL;
    size_t len = 0;
    size_t read = 0;
    while((read = getline(&line, &len, stdin)) > 1){
        if(read > EIGHTY){
            printf("%s\n", line);
        }
        read = 0;
    }
    return 0;
}

/* 第二种解法 */
#define _GNU_SOURCE
#include <stdio.h>
#define MAXLINE 1000  /* maximum input line size */
#define LONGLINE 80

int getlines(char s[], int lim);

/* print line longer than LONGLINE */
int main(){
    int len;         /* current line length */
    char line[MAXLINE]; /* current input line */
    while((len = getlines(line, MAXLINE)) > 0){
        if(len > LONGLINE){
            printf("%s", line);
        }
    }
    return 0;
}

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

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

1.19

/* 第一种解法 */
#include <stdio.h>

void printReverse(){
    int c;
    c = getchar();
    if(c == '\n'){
        return ;
    } else {
        printReverse();
        putchar(c);
        
    }
}

int main(){
    printReverse();
    printf("\n");
    return 0;
}

/* 第二种解法 */
#include <stdio.h>
#define MAXLINE 1000    /* maxinum input line size */

int getlines(char line[], int maxline);
void reverse(char s[]);

/* revserse input lines, a line at a time */
int main(){
    char line[MAXLINE];     /* current input line */
    while(getlines(line, MAXLINE) > 0){
        reverse(line);
        printf("%s", line);
    }
    return 0;
}

/*reverse: reverse string s*/
void reverse(char s[]){
    int i, j;
    char tmp;
    i = 0;

    while(s[i] != 0){   /* find the end of string s */
        ++i;
    }
    --i;                /* back off from '\0' */
    if(s[i] == '\n'){
        --i;            /* leave newline in place */
    }
    j = 0;              /* beginning of new string s */
    while(j < i){
        tmp = s[j];
        s[j] = s[i];
        s[i] = tmp;
        --i;
        j++;
    }
}

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

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

猜你喜欢

转载自blog.csdn.net/qq_33528164/article/details/87854648
今日推荐