Thread: remove trailing blanks

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    12

    remove trailing blanks

    Hello guys,
    As a beginner i have some problems with understanding k&r exercises. i came across the exercise: Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.
    What is a trailing blank? when i run the code and put some spaces and tabs in the input, nothing seems changed in the output. there's written somewhere: advance(head)==tail, how is this case possible? if i put "space" 3 times, head=3 and then if i put some letters like"abc", tail=3 and blank[0], blank[1], blank[2] will be printed which are "spaces". How is this removing? the same goes if i put spaces after the letters. The soluton to this which i found is here:
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    #define MAXQUEUE 1001
    
    
    int advance(int pointer)
    {
      if (pointer < MAXQUEUE - 1)
        return pointer + 1;
      else
        return 0;
    }
    
    
    int main(void)
    {
      char blank[MAXQUEUE];
      int head, tail;
      int nonspace;
      int retval;
      int c;
    
    
      retval = nonspace = head = tail = 0;
      while ((c = getchar()) != EOF) {
        if (c == '\n') {
          head = tail = 0;
          if (nonspace)
            putchar('\n');
          nonspace = 0;
        }
        else if (c == ' ' || c == '\t') {
          if (advance(head) == tail) {
            putchar(blank[tail]);
            tail = advance(tail);
            nonspace = 1;
            retval = EXIT_FAILURE;
          }
    
    
          blank[head] = c;
          head = advance(head);
        }
        else {
          while (head != tail) {
            putchar(blank[tail]);
            tail = advance(tail);
          }
          putchar(c);
          nonspace = 1;
        }
      }
    
    
      return retval;
    }
    Last edited by shotakobakhidze; 09-26-2015 at 03:29 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A trailing blank is a space (or tab) just before the newline.

    Eg.
    You type in "hello<space>world<space><space><newline>"

    What you need to produce is "hello<space>world<newline>"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    12
    How is it possible advance(head)==tail ?

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    12
    Bump. please could you show an example when advance(head)==tail? (from the code above)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It looks remarkably like this, with all the comments removed to disguise the source.
    https://github.com/jakubtuchol/knrc/.../chp1/ex18-2.c

    If you're serious about wanting to learn, write your own code and stop copy/pasting from google searches.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entab - replacing blanks with tabs and blanks
    By thames in forum C Programming
    Replies: 3
    Last Post: 10-20-2012, 03:11 PM
  2. Remove a specific char and remove multiple spaces
    By stam in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2010, 07:50 AM
  3. Fill the blanks in the program...
    By sreeramu in forum C Programming
    Replies: 19
    Last Post: 10-18-2007, 05:26 AM
  4. Fill in the blanks
    By Prelude in forum C++ Programming
    Replies: 22
    Last Post: 09-17-2006, 08:08 AM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM