Check string for blank spaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marvstyles
    New Member
    • Nov 2008
    • 4

    Check string for blank spaces

    How would i check a string for blank spaces and if there is more than one blank space between two words, how would i convert it into just one blank space between the words?


    Another question i have is.. how would u check a string to see if its a valid floating point-number?
  • Andr3w
    New Member
    • Nov 2007
    • 42

    #2
    Hey mate,

    All these that you ask I think you could possibly solve them if you saw the source code of a int to char manipulation function like atoi to toupper and so on.

    Now for your first question, in the ASCII table 0x20 (30 in decimal number system) is the white space (also known as spacebar :D) character so you could recursively (or not? depends on you) the string and check with a counter how many white spaces characters you find and if more than one is found then create a buff cut the first part of the string (including the first space) and then copy the renaming string excluding the first character (which would be our white space).

    First your second question now, that's a bit tricky. For integers you have the atoi function which does what you asked, but using floating point values is a bit hard because you can't predict how many decimal digits will be after the underscore (or coma depending on your writing style). An easy way to perform that operation for floats is to check the length of the string and then if it's between the decimal digit range of your data type (different for float or double) and then run a function to perform that conversion.

    a sample function would be this:
    Code:
    int strToNumberType( char *strPtr, int dataParseLength )
    {
    	int index = 1;
    
    	// check for null pointer
    	if( strPtr != NULL )
    	{
    		// now check for negative or positive numbers
    		if( *strPtr == 0x2B || *strPtr == 0x2D )
    		{
    
    			strPtr++;
    			index++;
    
    			while( *strPtr != '\0' && index <= dataParseLength ) {
    
    				if( *strPtr != 0x46 /* . writing */ || *strPtr != 0x2C /* , writing */ ||  *strPtr < 0x30 || *strPtr > 0x39 )
    				{
    					return FALSE;
    				}else {
    
    					// advance pointer and index
    					strPtr++;
    					index++;
    
    				}
    
    			}
    
    
    		}else {
    
    			// no sign provided
    			while( *strPtr != '\0' && index <= dataParseLength ) {
    
    				if( *strPtr != 0x46 /* . writing */ || *strPtr != 0x2C /* , writing */ ||  *strPtr < 0x30 || *strPtr > 0x39 )
    				{
    					return FALSE;
    				}else {
    
    					// advance pointer and index
    					strPtr++;
    					index++;
    
    				}
    
    			}
    
    		}
    	}else {
    
    		// invalid pointer
    		return FALSE;
    
    	}
    
    	// it's valid number
    	return TRUE;
    
    }

    I haven't tested it yet and it's not by any means optimal but it should do the job...i think :D

    Comment

    • Andr3w
      New Member
      • Nov 2007
      • 42

      #3
      Hey I can't seem to find a button to edit my previous post so I'll post a new one. If you don't want to take in account the length of the data type you want to check and the sign of the number ( only in case of positive sign ) you can use also the following code to check for your number

      Code:
      char buff[] = "15.000";
      float p;
      
      if ( sscanf( buff, "%f", &p ) <= 0 ) )
      {
        // error
      }else {
         // success now p would hold the float 15.000 in our case
         printf( "%f", p );
      }
      
      Hope this helps

      Comment

      Working...