Thread: Removing spaces from strings

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

    Removing spaces from strings

    I'm reading in very large numbers that may have unusual spacing... like 123 45 43657 4 And I need to store this number in an array of ints without all the spaces I'm really not very familiar with string manipulation so anything will help!!!

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Code:
    #include <stdio.h>
    
    int main() {
        int numbers[255];
    	   int x = 0;
        int numbers2[255];
    	   int y= 0;
    
        while(numbers[x] != '\0') {
    	   if(numbers[x] != ' ') {
    		  numbers2[y] = numbers[x];
    
    		  ++y;
    	   }
    
    	   ++x;
        }
        
        return 0;
    }
    look good?

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    It looks exactly like what I was trying to do... just so much cleaner! I'll try it out... Thanks!!!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read the input into a string (char array), then loop through each character, testing it with isdigit(), and then storing the number in the int array.


    >>while(numbers[x] != '\0') {
    Here, numbers[x] is unitialised. Also, if the data is in an int array, the its already number, so checking against ' ' is like checking against 32 (assuming ASCII).

    Maybe something in here will help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Hammer, going by the way you suggested would that method index a number more than one digit or would it only index a one digit number? Would it treat 106 as 106 or as 1, 0, 6?

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>Read the input into a string (char array), then loop through each character, testing it with isdigit(), and then storing the number in the int array.

    ADD::

    until a chararacter is NOT a digit then increment the array index

    if you know they are going to be a space then use

    isspace()
    Code:
    int	CheckStringForPunctuation(HWND hWnd,char *sBuffer)
    {
    	char				sTempBuffer[L_STRING], sNewString[L_STRING], *pNew=NULL,   *pOld=NULL;
    	int					iLen=0;
    
    	iLen=lstrlen(sBuffer);
    	if(iLen>=L_STRING)
    		return FALSE;//not enough space
    	
    	sprintf(sTempBuffer,"%s",sBuffer);
    	pOld=sTempBuffer;
    	pNew=sNewString;
    
    	while(*pOld != '\0')
    	{
    		while(ispunct(*pOld))	*pOld++;
    		while(isspace(*pOld))	*pOld++;
    		*pNew++=*pOld++;
    	}
    	*pNew='\0';
    	sprintf(sBuffer,"%s",sNewString);
    	return TRUE;
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by _Cl0wn_
    Hammer, going by the way you suggested would that method index a number more than one digit or would it only index a one digit number? Would it treat 106 as 106 or as 1, 0, 6?
    ANS: 1, 0, 6.
    Maybe that is the requirement, at least thats how I read it. But as usual, its open to interpretation.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading strings with spaces
    By dezz101 in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 06:02 AM
  2. reading strings with spaces using sscanf
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 06-14-2008, 05:06 PM
  3. Getting scaf to accept strings with spaces in them?
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 11-12-2005, 09:57 AM
  4. Removing spaces
    By chris1985 in forum C Programming
    Replies: 3
    Last Post: 12-22-2004, 09:23 AM
  5. accepting long strings with spaces
    By trang in forum C Programming
    Replies: 2
    Last Post: 01-05-2004, 04:17 PM