Thread: Removing blanks from beginning of string

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    Removing blanks from beginning of string

    Im trying to write a function rem_blanks( char * ) that takes in a char pointer,
    loops until it finds the first non space/tab char, then have the string pointer be the pointer to non blank char found. Here is my code.

    Code:
    void rem_blanks( char * string ){
            while( *string++ == ' ' || *( string - 1 ) == '\t' )
                    {       }
            return;
    }
    I thought that by incrementing the pointer in the function that it would leave pointer at the condition desired above, ie at the place of the first non-blank char.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The calling code will never see the change of the pointer.
    Kurt

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You still need to improve your question asking skills. There's actually no question in your post and it is unclear what "leave pointer at the condition desired above" means. But I think I know what you're asking.

    But first: YOU ARE TRYING TO DO TOO MUCH IN THE CONDITION OF THE WHILE ... again.
    Code:
    while (*string == ' ' || *string == '\t')
        string++;
    See how much simpler and clearer that is (as well as being more efficient) ?

    However, all you're doing is changing the local variable, which will not alter the caller's variable. Change the return type to char* and return it afterwards:
    Code:
    char *rem_blanks(char *string) {
        while (*string == ' ' || *string == '\t')
            string++;
        return string;
    }
    
    // elsewhere
        str = rem_blanks(str); // or whatever
    EDIT: Zuk beat me that time!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Thank you oogabooga and zuk!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-10-2010, 12:07 AM
  2. copy string not from beginning
    By Anubis208 in forum C++ Programming
    Replies: 3
    Last Post: 09-08-2008, 05:22 AM
  3. Replies: 5
    Last Post: 06-03-2002, 08:47 AM
  4. Removing '\n' From a String
    By Mace in forum C Programming
    Replies: 4
    Last Post: 12-02-2001, 07:42 AM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM