String to Long

This is a discussion on String to Long within the C Programming forums, part of the General Programming Boards category; int StringLen( char src[] ) { int len = 0; while( src[len] ) len++; return len; } long StringToLong( char ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    57

    String to Long

    int StringLen( char src[] )
    {
    int len = 0;
    while( src[len] )
    len++;
    return len;
    }

    long StringToLong( char src[] )
    {
    int len = StringLen(src),
    buffer = 0,factor=1;

    while ( len != 0 )
    {
    buffer += ((src[len-1] - '0')*factor);
    len--;
    factor *= 10;
    }
    return buffer;
    }


    this is what i have so far and i need it to return 0 if the string cant be converted. like if you put sdf as the string it should return 0

    any ideas?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    isdigit( ) should do the job.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Couldn't I do something with a character set like

    Code:
    char set[] = "1234567890";
    ?

    Code:
    int StringLen( char src[] )
    {
    int len = 0;
    while( src[len] )
    len++;
    return len;
    }
    
    long StringToLong( char src[] )
    {
    int len = StringLen(src),
    buffer = 0,factor=1;
    
    while ( len != 0 )
    {
    buffer += ((src[len-1] - '0')*factor);
    len--;
    factor *= 10;
    }
    return buffer;
    }

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Code:
    int StringLen( char src[] )
    {
    	int len = 0;
    	while( src[len] )
    		len++;
    	return len;
    }
    
    int StringSpn( char str[], char set[] )
    {
    	int i = 0;
    	int j = 0;
    	int found = 0;
    
    	while( str[i] )
    	{
    		while( (set[j] != '\0') && (str[i] != set[j]) )
    		{
    			j++;
    		}		
    		
    		if ( str[i] == set[j] )
    		{
    			found++;
    			j++;
    		}
    		else
    		{
    			return 0;
    		}
    	
    		i++;
    	}
    	
    	return 1;	
    }
    
    long StringToLong( char src[] )
    {
    	char set[] = "1234567890";
    	int j = 0;
    	int len = StringLen(src);
    	int buffer = 0;
    	int factor = 1;
    
    	if( StringSpn(src, set) == 0 )
    		return 0;
    	else
    	{
    		while ( len != 0 )
    		{
    			buffer += ((src[len-1] - '0')*factor);
    			len--;
    			factor *= 10;
    		}
    
    		return buffer;
    	}
    }

    How change this to work for a double StringToDouble( char src[] ); function?

  5. #5
    Registered User thePope's Avatar
    Join Date
    Mar 2003
    Posts
    26
    How change this to work for a double StringToDouble( char src[] ); function?
    I'm not sure if i understood your question correctly but if you just wanted to turn a string to double you could just use atof()

    sorry if i misunderstood...by the way i think atof will just return something crappy if you try to put 'adf' in it or something...i would just use isdigit() and if it's okay then just use atof()

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    strtod() would be better. Use it the same way as strtol() is used in these examples.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    But, for example what if those didn't exist. Then what would you do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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