Thread: To remove Trailing Blanks,tabs. program

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    31

    To remove Trailing Blanks,tabs. program

    is there any other way how to use int removetrail(char s[]) without loops FOR and IF

    Code:
    intremovetrail(chars[]){
      int i;
    
      for(i=0; s[i]!='\n'; ++i)
        ;
      --i;  /* To consider raw line without \n */
    
      for(i >0; ((s[i] == ' ') || (s[i] =='\t'));--i)   
        ; /* Removing the Trailing Blanks and Tab Spaces */
    
      if( i >= 0) /* Non Empty Line */
      {
        ++i;
        s[i] = '\n';
        ++i;
        s[i] = '\0';
      }
      return i; }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,729
    Yes Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    Can you please tell me how to put it on my code i am new in C language thank you

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,609
    Look in your book for other loop constructs.
    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.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,729
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define STRING_SIZE 100
    
    int removetrail(char s[]);
    
    int main()
    {
        int result;
    
        char mystring [STRING_SIZE+1];
    
        printf("Enter line of input: ");
    
        fgets ( mystring, STRING_SIZE, stdin );
    
        result = removetrail(mystring);
    
        if (result > 1) mystring[result-1] = 0;
    
        printf("[%s]\n", mystring);
        return 0;
    }
    
    int removetrail(char s[])
    {
        int i;
    
        for(i=0; s[i]!='\n'; ++i)
            ;
        --i;  /* To consider raw line without \n */
    
        for(i >0; ((s[i] == ' ') || (s[i] =='\t')); --i)
            ; /* Removing the Trailing Blanks and Tab Spaces */
    
        if( i >= 0) /* Non Empty Line */
        {
            ++i;
            s[i] = '\n';
            ++i;
            s[i] = '\0';
        }
        return i;
    }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    Easier way is iterate through the string from right to left.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <assert.h>
    
    /*
      removes trailing ws from txt
    */
    void trim_right(char txt[])
    {
      size_t len = strlen(txt);
    
      if (len == 0)
        return;
    
      len--; /* skip '\0' */
      while (len > 0 && isspace(txt[len]))
        len--;
    
      if (len == 0)
        txt[len] = '\0';
      else
        txt[len+1] = '\0';
    }
    
    int main()
    {
      char txt[] = "Hello \t\n ";
      trim_right(txt);
      assert(strcmp("Hello", txt) == 0);
    
      char txt2[] = "";
      trim_right(txt2);
      assert(strcmp("", txt2) == 0);
    
      char txt3[] = "Hello";
      trim_right(txt3);
      assert(strcmp("Hello", txt3) == 0);
    
      char txt4[] = "\n\t\r";
      trim_right(txt4);
      assert(strcmp("", txt4) == 0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. remove trailing blanks
    By shotakobakhidze in forum C Programming
    Replies: 4
    Last Post: 09-27-2015, 12:12 AM
  2. Replies: 3
    Last Post: 02-28-2014, 01:51 PM
  3. Entab - replacing blanks with tabs and blanks
    By thames in forum C Programming
    Replies: 3
    Last Post: 10-20-2012, 03:11 PM
  4. Replies: 1
    Last Post: 02-27-2012, 06:38 AM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM

Tags for this Thread