练习1-18在K&放大器; R C程序设计语言 [英] Exercise 1-18 in K&R The C Programming Language

查看:132
本文介绍了练习1-18在K&放大器; R C程序设计语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本练习快把我逼疯了。很抱歉,如果我的编程问题显得小气,但我是一个新手,所以请多多包涵。

演习要求如下:
编写一个程序,删除输入的每一行尾随空格和制表符,并删除全部空行。

谁能告诉我为什么我的code不起作用?我还没有解决的完全空白行的问题,但我找不到任何理由,为什么我的code不会删除尾随空格和制表符。在这里,它是:

 的#include<&stdio.h中GT;#定义MAXLINE 1000INT better_line_length(焦线[],INT LIM);
无效副本(字符为[],在[]字符);INT主要(无效)
{
    INT LEN;
    个char [MAXLINE]
    焦炭better_s [MAXLINE]    而((LEN = better_line_length(S,MAXLINE))大于0){
        副本(better_s,S);
        的printf(%S \\ n,better_s);
    }    返回0;
}INT better_line_length(char中[],INT LIM)
{
    INT C,I,last_letter;    对于(i = 0; I< LIM - 1安培;及(C =的getchar())= EOF和放大器;和C ='\\ n'; ++我!){
        S [i] = C;
        如果(C =!&放大器;和C =!'\\ T')
            last_letter = I;
    }    S [last_letter + 1] ='\\ 0';    返回last_letter + 1;
}无效副本(字符为[],在[]字符)
{
    INT I = 0;    而((在[I] =从[I])!='\\ 0')
        ++我;
}

例如,我希望code的工作如下:如果我的输入是这样的(其中▯表示空格):

 abcdefg▯▯▯▯▯▯▯▯▯▯▯▯

我希望可以将输出为

  ABCDEFG

请注意,有没有空格预期输出后。

但相反,输出我得到的是输入的第一个字符的无限打印。

更新:我的code工作时,输入与键盘作为预期,但我仍然可以当输入来自另一个文件中获取一个字符的重复。我的心被烧断。


解决方案

问题是你的code的这一部分:

 ,而((LEN = better_line_length(S,MAXLINE))0){
    副本(better_s,S);
    的printf(%S \\ n,better_s);
}

这无限迭代为(LEN = better_line_length(S,MAXLINE))将始终大于0(字符串至少有1个字符)。<结果/ p>

您不需要,而循环。相反,像这样做:

  LEN = better_line_length(S,MAXLINE);
副本(better_s,S);
的printf(%S | \\ n,better_s);

(也请注意,您不使用 LEN 的任何地方)

This exercise is driving me crazy. Sorry if my programming problems seem petty, but I'm a beginner so please bear with me.

The exercise asks for the following: Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

Can anybody tell me why my code doesn't work? I haven't yet tackled the issue of entirely blank lines, but I can't find any reason why my code won't remove trailing blanks and tabs. Here it is:

#include <stdio.h>

#define MAXLINE 1000

int better_line_length(char line[], int lim);
void copy(char to[], char from[]);

int main(void)
{
    int len;
    char s[MAXLINE];
    char better_s[MAXLINE];

    while ((len = better_line_length(s, MAXLINE)) > 0) {
        copy(better_s, s);
        printf("%s\n", better_s); 
    }

    return 0;
}

int better_line_length(char s[], int lim)
{
    int c, i, last_letter;

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

    s[last_letter+1] = '\0';

    return last_letter+1;
}

void copy(char to[], char from[]) 
{
    int i = 0;

    while ((to[i] = from[i]) != '\0')
        ++i;
}

For example, I expect the code to work as follows: If my input is like this (where '▯' indicates a space):

abcdefg▯▯▯▯▯▯▯▯▯▯▯▯

I expect the output to be

abcdefg

Notice that there are no spaces after the expected output.

But instead, the output I get is an infinite printing of the first character of the input.

UPDATE: My code works as expected when input is taken from the keyboard, but I still get the repeating of one character when input is taken from another file. My mind is blown.

解决方案

The problem is this part of your code:

while ((len = better_line_length(s, MAXLINE)) > 0) {
    copy(better_s, s);
    printf("%s\n", better_s); 
}

It iterates infinitely as the result of (len = better_line_length(s, MAXLINE)) will always be greater than 0 (for strings with at least 1 character).

You do not need a while loop. Instead, do it like this:

len = better_line_length(s, MAXLINE);
copy(better_s, s);
printf("%s|\n", better_s); 

(also note that you don't use len anywhere)

这篇关于练习1-18在K&放大器; R C程序设计语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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