简体   繁体   中英

K&R Exercise 1-18 Almost done… Why am i getting strange characters at the end of some lines?

This is my code. exercise 1-18 says: Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

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

when i deleted the commented lines that eclipse adds it compiled...

#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';
}

and this is the output i get when in terminal i write ./myprogram < 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

This is my final code for this exercise...

#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';
}

Works like a charm. Thx @vaughn cato and @R sahu you were both right and that was my mistake in some lines.

copy() is wrong:

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';
}

For example, let's say that from is "abc". The loop will end when i is equal to 3, because the statement from[i] != '\\0' will be false . In that case, you want to put the nul terminator at to[3] not to[4] .

I think this line, in function copy(), is not right.

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

It should be:

to [i] = '\0';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM