Thread: How to remove end spaces from string

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    28

    How to remove end spaces from string

    Hi,
    I am new to this forum.Can anybody please tell me how to remove end spaces (whitespaces) from a string.

    Thank You,
    Nitin

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    char *p;
    while (*p != '\n') p++;
    *p = '\0';

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You can start from the last character of the string
    then in the loop
    check if it is whitespace
    and if condition is true - put there '\0' character and move to the previous character.
    if you find a non-whitespace character or get to the beginning of the string - you should exit the loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    28

    Thank you very much

    Thank u very much for help.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int trim( char *s )
    {
        if( s )
        {
            if( !*s || (trim( s + 1 ) && isspace( *s )) )
            {
                *s = '\0';
                return 1;
            }
        }
        return 0;
    }
    Don't turn it in if you can't explain it.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. copy a string in the end of another string!?
    By hyaku_ in forum C Programming
    Replies: 9
    Last Post: 11-29-2006, 06:22 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM