Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions with strings

Status
Not open for further replies.

Krusho

Programmer
Mar 31, 2003
13
0
0
US
Could someone show me the correct way to implement the following functions in C?

double StringToDouble(char src[]);

// StringToDouble converts a number stored in the string src to its floating-point representation, if possible

long StringToLong (char src[]);

// StringToLong works like StringToLong except that it converts a number in the string src to a long, if possible



No replies found.

 
Yes, but I am required to write them myself and I can't see the actual code for those other ones.
 
First you need to write down which format you expect. E.g. leading + or - character, followed by a series of digits (for Long conversion). For double, optionally followed by a .-symbol and (mandatory) a series of digits. You need to consider if you support numbers such as .3

For long, take one character at a time (scanning from left to right), multiply the intermediate result by 10 (which is the base of the numbers) and add the value of the character.

For double you need to do some additional stuff (checking on .-character, processing the following digits).

Obviously, if you want to support the 10E4 notation, you need additional coding.

Mind overflows.

Hope this helps.

Pieter
 
And remember that the ascii code for the character "0" is not 0. The easiest thing to do to convert an ascii character to its numerical value is to subtract the ascii code for "0".
 
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”.
 
did you do stringtointeger.

Id start there and then modify it as necessary. would a lookup table help?

just asking.


tomcruz.net
 
Just some thoughts:

Are you familiar with the isdigit() and isspace() macro's? (look at the manual page).

Wouldn't it be easier to advance through the input, rather than work backwards?

You need to look at what the C-constants are all about. Look at the syntax. Write a small scanner. You can almost decide on a per character basis which next step to take.

Some checks will be
- which base (octal, hexadecimal, decimal)
- which valid digits (0-7, 0-9&A-F, 0-9)
- sign character available?
- what is the stop criterion
- any further specifier available (U, L)

HTH


 
I'm still really new and our teacher is teaching based on the assumption everyone has had previous experience. Could you guys walk me through this?
 
The C "Integer and Floating point syntax" is nicely explained on the following page.


It shows a nice flow of how an integer number can be seen as a series of different characters. This can be used as the basis of your program. It also shows floating point sequence.

Hope this puts you in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top