K& R练习1-18差不多完成了...为什么我在一些行的末尾会得到奇怪的字符?

时间:2015-03-19 03:00:23

标签: c

这是我的代码。 练习1-18说:编写程序以从每行输入中删除尾随空白和制表符,并删除完全空行。

this is the post i made like an hour ago for this exercise

当我删除eclipse添加编译的注释行时......

#include <stdio.h>

#define MAXLINE 1000
#define BLANK 1
#define NOT_BLANK 0

int getlinea (char s [], int lim);
void copy (char to [], char from []);
int erasewhites (char s[], int len);

int main (void){
    char palabra [MAXLINE];
    int len;
    while ( (len = getlinea (palabra, MAXLINE) ) >= 0)
        if (len > 0)
            printf ("%s\n",palabra);
    return 0;
}

int getlinea (char s[], int lim){
    int i, c, len;
    char aux[MAXLINE];
    for ( i=0; ( c = getchar () )!= EOF && c != '\n' && i < lim-1 ; ++i)
        aux[i] = c ;
    if (c == EOF)
        return -1;
    if ( ( len = erasewhites ( aux, i) ) > 0 )
        copy ( s , aux );
    else
        return 0;
    return len;
}

int erasewhites (char s[], int len){
    int state = BLANK;
    while (len >= 0 && state == BLANK){
        if ( s[len] != 32 && s[len] != '\t' )
            state = NOT_BLANK;
        else
            --len;
    }
    if ( len >= 0 )
        s[len+1] = '\0';
    else
        return 0;
    return len;
}

void copy ( char to[], char from [] ){
    int i;
    for ( i = 0 ; from [i] != '\0' ; ++i)
        to [i] = from [i] ;
    to [i+1] = '\0';
}

这是我在写入终端时得到的输出 ./myprogram&lt; main.c中

#include <stdio.h>t
#define MAXLINE 1000
#define BLANK 1 
#define NOT_BLANK 00
int getlinea (char s [], int lim);
void copy (char to [], char from []);
int erasewhites (char s[], int len);;
int main (void){(c
    char palabra [MAXLINE];],
    int len;ab
    while ( (len = getlinea (palabra, MAXLINE) ) >= 0)d
        if (len > 0)= 
            printf ("%s\n",palabra);al
    return 0;("
}re
int getlinea (char s[], int lim){, 
    int i, c, len;ha
    char aux[MAXLINE];s[
    for ( i=0; ( c = getchar () )!= EOF && c != '\n' && i < lim-1 ; ++i)
        aux[i] = c ;c 
    if (c == EOF)c 
        return -1;F)
    if ( ( len = erasewhites ( aux, i) ) > 0 )= 
        copy ( s , aux );wh
    elsey 
        return 0;, 
    return len; 
}re
int erasewhites (char s[], int len){) 
    int state = BLANK;ar
    while (len >= 0 && state == BLANK){) 
        if ( s[len] != 32 && s[len] != '\t' ) 
            state = NOT_BLANK;& 
        elsete
            --len;= 
    }   
    if ( len >= 0 )LA
        s[len+1] = '\0';NK
    elseen
        return 0;= 
    return len; 
}re
void copy ( char to[], char from [] ){) 
    int i;py
    for ( i = 0 ; from [i] != '\0' ; ++i)) 
        to [i] = from [i] ;i]
    to [i+1] = '\0';i]
}to

这是我练习的最终代码......

#include <stdio.h>

#define MAXLINE 1000
#define BLANK 1
#define NOT_BLANK 0

int getlinea (char s [], int lim);
void copy (char to [], char from []);
int erasewhites (char s[], int len);

int main (void){
    char palabra [MAXLINE];
    int len;
    while ( (len = getlinea (palabra, MAXLINE) ) >= 0)
        if (len > 0)
            printf ("%s\n",palabra);
    return 0;
}

int getlinea (char s[], int lim){
    int i, c, len;
    char aux[MAXLINE];
    for ( i=0; ( c = getchar () )!= EOF && c != '\n' && i < lim-1 ; ++i)
        aux[i] = c ;
    if (c == EOF)
        return -1;
    if ( ( len = erasewhites ( aux, i) ) > 0 )
        copy ( s , aux );
    else
        return 0;
    return len;
}

int erasewhites (char s[], int len){
    int state = BLANK;
    while (len >= 0 && state == BLANK){
        if ( s[len] != 32 && s[len] != '\t' )
            state = NOT_BLANK;
        else
            --len;
    }
    if ( len >= 0 )
        s[len] = '\0';
    else
        return 0;
    return len;
}

void copy ( char to[], char from [] ){
    int i;
    for ( i = 0 ; from [i] != '\0' ; ++i)
        to [i] = from [i] ;
    to [i] = '\0';
}

像魅力一样工作。感谢@vaughn cato和@R sahu你们都是对的,这是我在某些方面的错误。

2 个答案:

答案 0 :(得分:2)

copy()错了:

void copy ( char to[], char from [] ){
    int i;
    for ( i = 0 ; from [i] != '\0' ; ++i)
        to [i] = from [i] ;
    to [i+1] = '\0'; // should be to [i] = '\0';
}

例如,让我们说from是&#34; abc&#34;。当i等于3时,循环将结束,因为语句from[i] != '\0'将为false。在这种情况下,您希望将nul终结符设为to[3]而不是to[4]

答案 1 :(得分:2)

我认为函数copy()中的这一行是不对的。

to [i+1] = '\0';

应该是:

to [i] = '\0';