Instructions: Trailing blanks and tabs are defined as blanks and tabs that precede a '\n' character or an EOF to terminate a line, without any intervening characters of other kinds. You should copy a getline() function of the kind you see in the text to read in lines from stdin into an array, trim the line, and then write out the trimmed line to stdout, continuing until you encounter an EOF.
Note: If there are 4 non-blank lines in the input to trim, there should be 4 lines in the output, but lines consisting of only blanks, and tabs, should NOT be output. The trim function should not affect any blanks or tabs that do not come at the end of the line.

I have a runtime error. I octal dumped the input file and used the input file with the code I wrote to produce an output file and then octal dumped it. However, my octal dump of my out file is completely wrong! Somewhere in my program there is a non terminating loop; because I had to control C.
Here is the execution:

blade71(136)% gcc trim_3.c
blade71(137)% od -x trim.in
0000000 0909 4e6f 7720 6973 2074 6865 2020 2020
0000020 2020 200a 0909 5469 6d65 2066 6f72 0909
0000040 0a09 0961 6c6c 2067 6f6f 6420 6d65 6e20
0000060 746f 2020 2020 0a09 0963 6f6d 6520 746f
0000100 2074 6865 2061 6964 206f 6620 7468 6520
0000120 7061 7274 792e 2020 2020 2020 0a0a
0000136
blade71(138)% gcc -o trim trim_3.c
blade71(144)% dir
In_Files core trim trim.c trim.in trim_2.c trim_3.c
blade71(145)% trim<trim.in>trim.out
^C
blade71(146)% od -x trim.out
0000000 ffff ffff ffff ffff ffff ffff ffff ffff
*
12260000

Here is the final source:

blade71(171)% cat trim_3.c
#include<stdio.h>
#define MAXLINE 1000

int getline (char line[], int maxline);
int copy_NW(char to[], char from [], int NWS);

main ()
{
        int NL, WS, len;
        char line[MAXLINE];
        char line_NW[MAXLINE];
        while ((len = getline(line, MAXLINE))>0)
                {
                        WS++;
                }

        while(WS>1)
                {
                        copy_NW(line, line_NW, WS);
                }
return 0;


}

int getline(char s[], int lim)
{
        int c, i;
        for (i = 0; i<lim -1 && (c=getchar())!= EOF && c!='\n'; ++i)
                s[i]= c;
        if (c=='\n'){
                s[i] = c;
                ++i;
                        }
                s[i] = '\0';
                return i;
}

int copy_NW(char line[],  char line_NW[], int NL)
{
        int c, d;
        int i = 0;
        while(NL != 0 && (c=getchar())!=EOF)
        line[i] = line_NW[i];
        if(c==' ' || c=='\n' || c=='\t')
        NL--;
        d=putchar(c);
        line_NW[i] = d;
        return d;
}

Recommended Answers

All 4 Replies

while ((len = getline(line, MAXLINE))>0)
                {
                        WS++;
                }

        while(WS>1)

What is the value of WS here? I mean, before you do the increment?

while ((len = getline(line, MAXLINE))>0)
                {
                        WS++;
                }

        while(WS>1)

What is the value of WS here? I mean, before you do the increment?

it should be set to 0 before manipulation of any data. Thank you

still is infinitely looping though

Solved and completely re-done:

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 1000

/* This program will remove all trailing white space from an input file.
   Using a loop containing several if statements to check for the ASCII
      value of any white space generating characters; the program will
         individually access each character until it reaches the EOF.    */

int read(int index)
{
  if (index < MAXLINE - 1)
      return index + 1;
        else
            return 0;
}

int main(void)
{
        char S[MAXLINE];
        int head, tail, White_Space, Return_Value, c;
        Return_Value = White_Space = head = tail = 0;

        while ((c = getchar()) != EOF)
        {
                if (c == '\n')
                {
                head = tail = 0;
                if (White_Space)
                putchar('\n');
                White_Space = 0;
                }
                else if (c == ' ' || c == '\t')
                {
                        if (read(head) == tail)
                        {
                        putchar(S[tail]);
                        tail = read(tail);
                        White_Space = 1;
                        Return_Value = EXIT_FAILURE;
                        }
                S[head] = c;
                head = read(head);
                }

                else
                {
                while (head != tail)
                        {
                        putchar(S[tail]);
                        tail = read(tail);
                        }
                        putchar(c);
                        White_Space = 1;
                }
        }
        return Return_Value;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.