Thread: Getting rid of spaces in a string

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Getting rid of spaces in a string

    Simple question.
    Example:
    I want the string
    "I like pie"
    to become
    "Ilikepie"

    Is there a function for this?
    I've tried google with no success.

    Thanks in advance.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    No, but it's easy to write.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here's a crappy function that i just wrote:

    Code:
    //The string must be NULL terminated
    void StripSpaces( char *buf )
    {
    	char *ptr = buf;
    	int count = 0;
    
    	//Loop till we find NULL
    	while( *ptr ){
    		if( *ptr == ' ' ){
    			//Shift the string
    			while( ptr[count] = ptr[count+1] )
    				count++;
    			count = 0;
    		}
    		else{
    			//Move to the next character
    			ptr++;
    		}
    	}
    }
    Last edited by The Dog; 10-23-2003 at 06:10 AM.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    I think I just solved it.

    Thanks a lot anyway!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM