string manipulation

A

avida

Hello,
I have to create a fix length string out of price string I'm getting;
what I have to do is the follow: the string I'm getting is a price
string can be maximum 8 charecters length out of it I have to create 9
charecters string 6 charecters for the whole number (with leading
zeros) a dot in the 7th possition and 2 for the decimal numbers(zeros
if none)j.
I'll be gratefull for some help :)
 
M

mark_bluemel

avida said:
Hello,
I have to create a fix length string out of price string I'm getting;
what I have to do is the follow: the string I'm getting is a price
string can be maximum 8 charecters length out of it I have to create 9
charecters string 6 charecters for the whole number (with leading
zeros) a dot in the 7th possition and 2 for the decimal numbers(zeros
if none)j.
I'll be gratefull for some help :)

Fair exchange is no robbery. You fix the system crash I'm debugging and
I'll write your code.

Otherwise, try doing some work yourself (look at the description of
things like sscanf() for parsing your numbers and printf() for
formatting, perhaps?) and come to us for help, showing us what you've
done and explaining the problems you've encountered.
 
M

Martin Ambuhl

avida said:
Hello,
I have to create a fix length string out of price string I'm getting;
what I have to do is the follow: the string I'm getting is a price
string can be maximum 8 charecters length out of it I have to create 9
^^
10
charecters string 6 charecters for the whole number (with leading
zeros) a dot in the 7th possition and 2 for the decimal numbers(zeros
if none)j.

and one for the terminating '\0', without which your character array is
not a string at all.
I'll be gratefull for some help :)

Sure:
char price_string[10];
 
A

avida

Fair exchange is no robbery. You fix the system crash I'm debugging and
I'll write your code.

Otherwise, try doing some work yourself (look at the description of
things like sscanf() for parsing your numbers and printf() for
formatting, perhaps?) and come to us for help, showing us what you've
done and explaining the problems you've encountered.


sorry :) I didn't mean to be rude, and didn't want no one to do my
work just guiding and advising of how to start...;
thanks for your help i think the sscanf() function will help me.
I'll write the code and then come back with a specific problem (if I'll
have one :)

thanks
 
M

mark_bluemel

avida said:
(e-mail address removed) wrote: .... ....
sorry :) I didn't mean to be rude, and didn't want no one to do my
work just guiding and advising of how to start...;
thanks for your help i think the sscanf() function will help me.
I'll write the code and then come back with a specific problem (if I'll
have one :)

Sorry, I was having a funny turn. Have a try and get back to us, we (or
at least I) will try to be helpful.
 
M

Mark McIntyre

Hello,
I have to create a fix length string out of price string I'm getting;
what I have to do is the follow: the string I'm getting is a price
string can be maximum 8 charecters length out of it I have to create 9

you need ten.
charecters string 6 charecters for the whole number (with leading
zeros) a dot in the 7th possition and 2 for the decimal numbers(zeros
if none)j.

and one for the trailing \0, to make sure its a string.
I'll be gratefull for some help :)

Did you check on the format specifiers to sprintf? You should find
that a combination of these will do what you want. If not, you can
break your price into the integer and decimal parts, and recombine
them.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Malcolm

avida said:
sorry :) I didn't mean to be rude, and didn't want no one to do my
work just guiding and advising of how to start...;
thanks for your help i think the sscanf() function will help me.
I'll write the code and then come back with a specific problem (if I'll
have one :)
What you have got to understnad is that C strings are very low level.
Basically a string in C is just a series of bytes in memory, terminated with
a NUL, but otherwise with no control structure whatsoever. This differs from
most other languages.

When you call scanf(), it just reads characters into a buffer. It has no way
of knowing how big the buffer is unless you tell it. However if you make the
buffer very large, say 1024 characters, then it will hold pretty much
anything entered by a normal user.

Ypu can then find the size of the string and, if short enough, copy it to a
smaller buffer.

Buffers can be declared two ways

char mystring[1024];

or
char *dynamicstring;

dynamicstring - malloc(1024);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top