This is what i have so far for the StringToLong()
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 )
{
while( (set[j] != '\0') && (str != set[j]) )
{
j++;
}
if ( str == 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;
}
}
But how could I make that fully work with the assignment?
Assignment:
StringToLong works like StringToLong except that it converts a number in the string src to a long, which it returns. The contents of the string must match the expected number model as defined by C’s integer constant syntax. It scans the character string src, ignoring any leading whitespace characters, until it finds a character that matches C’s integer constant syntax. Scanning of the string continues until a character that is not a valid part of the number is encountered, or the end of the string is reached. If no conversion is possible because the string does not match C’s integer constant syntax, a value of zero is returned. Otherwise, the number is then converted and the value returned. Here are some examples of strings containing valid integer constants: “0”, “077”, “0xabcdef12”, “0x7U, “7U”, “7UL”, “7LU”, “0x7u”, “7u”, “7ul”, “7lu”.