这是否小的C程序满足K&放大器; R运动? [英] Does this small C program satisfy the K&R exercise?

查看:140
本文介绍了这是否小的C程序满足K&放大器; R运动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到K&安培; R的运动1-18

I'm on to K&R's Exercise 1-18

编写一个程序,删除输入的每一行尾随空格和制表符,并删除全部空行。

Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

这就是我想出迄今

#include <stdio.h>

#define MAXLINE 1000

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

int main () {

    int len;
    char line[MAXLINE];

    while (getline(line, MAXLINE) > 0) {
            printf("%s", line);
    }
    return 0;
}


int getline(char s[], int lim) {
    int c, i, lastNonBlankIndex;
    lastNonBlankIndex = 0;

    for (i=0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {

        if (c != ' ' && c != '\t') {
            lastNonBlankIndex = i + 1;

        } 

        s[i] = c;
    }

    if (i != lastNonBlankIndex) {
        i = lastNonBlankIndex;
        c = '\n';
    }

    if (c == '\n') {
        s[i] = c;   
        ++i;
    }
    s[i] = '\0';
    return i;
}

第二部分响起辛苦,因为我不知道我应该返回什么,如果该行只有空格或制表符。毕竟,如果我返回0,它将停止函数getline()调用。这会是我应该成立一个的#define ,如 ALL_BLANKS

The second part sounded hard, as I wasn't sure what I should return if the line only has blanks or tabs. After all, if I return 0, it will halt the getline() calling. Would this be where I should set up a #define, such as ALL_BLANKS.

总之,实际主要问题,这是一个正确的方式来去除线尾随空白和标签?我跑了几个输入,通过和它的显得的工作。但是,如果我复制并用换行到CL粘贴文本,它的出现都串在一起。当我键入​​一行到CL和按回车,自动打印它。我应该建行的数组,然后通过循环进行打印完成时?

Anyway, to actual main question, is this a correct way to remove trailing blanks and tabs from lines? I ran a few inputs through, and it seemed to work. However, if I copy and paste text with newlines into the CL, it appears all strung together. And when I type a line into the CL and push enter, it automatically prints it. Should I be building an array of lines, and then looping through and printing them when done ?

推荐答案

您code看起来是正确的,但我认为它会更好,如果你单独从标准输入<读一本线的操作/ code>和去除尾随空白(脱钩)的路线。然后你可以使用未经修改的函数getline 从书(code重用),并不会停止在返回0的问题。

Your code looks correct, but I think it would be better if you separate the operations of reading a line from stdin and stripping the line of trailing whitespace (decoupling). Then you can use the unmodified getline from the book (code reuse) and won't have the problem of halting on returning 0.

如果你有兴趣在其他的解决方案,在CLC-wiki有 K&放一个几乎完整列表; R2解决方案

And if you are interested in other solutions, the CLC-wiki has an almost complete list of K&R2 solutions.

#include <stdio.h>
#define MAXLINE 1024

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

main()
{
    int i, len;
    char line[MAXLINE];

    while ((len = getline(line, MAXLINE)) > 0) {
        i = len - 2;
        while (i >= 0 && (line[i] == ' ' || line[i] == '\t'))
            --i;
        if (i >= 0) {
            line[i+1] = '\n';
            line[i+2] = '\0';
            printf("%s", line);
        }
    }
    return 0;
}

这是第1类解决方案,我写了前一段时间。 函数getline 是这本书的第28页上。这可能是更好的把去除空白的一个单独的函数 rstrip ,但在我离开这个作为一个练习留给读者。

This is the category 1 solution I wrote some time ago. getline is as on page 28 of the book. It might be nicer to put the removal of whitespace in a separate function rstrip, but I leave this as an exercise for the reader.

这篇关于这是否小的C程序满足K&放大器; R运动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆