How to remove end spaces from string

This is a discussion on How to remove end spaces from string within the C Programming forums, part of the General Programming Boards category; Hi, I am new to this forum.Can anybody please tell me how to remove end spaces (whitespaces) from a 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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 03: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, 05:22 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21