Need help with Simple C strings 
Author Message
 Need help with Simple C strings

Could someone please direct me on where I can find some help with
strings? I have an assignment (listed below) to complete and I'm still
a newbie with Simple C.

Thanks!!!
..:: Ken ::..

**** Copy of assignment ****
INPUT:
        A STRING (up to 81 CHARACTERS) THAT CONSISTS OF THE FOLLOWING
DATA
                quantity on hand                4 positions    
                description                     25 positions
                unit cost                       5 positions
                Damaged goods           3 positions
                location                        3 positions
                purchase date           6 positions
                part number             8 positions
                serial number           20 positions

        the fields are separated by commas.
                example:
24,#2 Phillips screwdriver,.79,0,W62,082301,hts02p6m,0

OUTPUT:

        The part#, description, and total value of the inventory for
that part(dollars and cents)

PROCESSING:

        separate the desired fields from the input string and convert
the numeric data into binary values in the program.  Calculate the
total inventory value as the QUANTITY ON HAND minus the DAMAGED GOODS
multiplied by the UNIT COST.          unit cost = (quantity on hand -
damaged goods) * unit cost

You decide what the spacing and format should be for the outputs, but
remember that the total inventory value is expressed in dollars and
cents.



Fri, 04 Mar 2005 07:08:04 GMT  
 Need help with Simple C strings

Quote:

>Could someone please direct me on where I can find some help with
>strings? I have an assignment (listed below) to complete and I'm still
>a newbie with Simple C.

Its a common rule here that we don't do homework /for/ people, we
expect them to have a go. I realise you may be totally stuck but I
strongly suggest that you write some code yourself, even if it doesnt
work ,and then ask for help to fix it.

BTW if this were my project, I'd be looking at fopen, fgets, strtok, a
for loop, printf and fclose in roughly that order.  Other people may
object to strtok, and its true it can be dangerous, but it works.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Fri, 04 Mar 2005 07:20:52 GMT  
 Need help with Simple C strings

Quote:



>>Could someone please direct me on where I can find some help with
>>strings? I have an assignment (listed below) to complete and I'm still
>>a newbie with Simple C.

> Its a common rule here that we don't do homework /for/ people, we
> expect them to have a go. I realise you may be totally stuck but I
> strongly suggest that you write some code yourself, even if it doesnt
> work ,and then ask for help to fix it.

> BTW if this were my project, I'd be looking at fopen, fgets, strtok, a
> for loop, printf and fclose in roughly that order.  Other people may
> object to strtok, and its true it can be dangerous, but it works.

I tend to use strchr to do that kind of parsing, finding the commas then
replacing them with '\0', and keeping a pointer to the next character
at the same time.

        - Kevin.



Fri, 04 Mar 2005 09:30:38 GMT  
 Need help with Simple C strings
On Mon, 16 Sep 2002 00:20:52 +0100, Mark McIntyre

Quote:



>>Could someone please direct me on where I can find some help with
>>strings? I have an assignment (listed below) to complete and I'm still
>>a newbie with Simple C.

>Its a common rule here that we don't do homework /for/ people, we
>expect them to have a go. I realise you may be totally stuck but I
>strongly suggest that you write some code yourself, even if it doesnt
>work ,and then ask for help to fix it.

>BTW if this were my project, I'd be looking at fopen, fgets, strtok, a
>for loop, printf and fclose in roughly that order.  Other people may
>object to strtok, and its true it can be dangerous, but it works.

Thanks for the info.... I will give it a try and see what happens.

BTW I only added a copy of my assignment for a reference, not to have
some write the code for me. In the future I will try to be more clear
in my message.

Ken



Fri, 04 Mar 2005 11:14:40 GMT  
 Need help with Simple C strings
On Mon, 16 Sep 2002 01:30:38 +0000 (UTC), Kevin Easton

Quote:




>>>Could someone please direct me on where I can find some help with
>>>strings? I have an assignment (listed below) to complete and I'm still
>>>a newbie with Simple C.

>> Its a common rule here that we don't do homework /for/ people, we
>> expect them to have a go. I realise you may be totally stuck but I
>> strongly suggest that you write some code yourself, even if it doesnt
>> work ,and then ask for help to fix it.

>> BTW if this were my project, I'd be looking at fopen, fgets, strtok, a
>> for loop, printf and fclose in roughly that order.  Other people may
>> object to strtok, and its true it can be dangerous, but it works.

>I tend to use strchr to do that kind of parsing, finding the commas then
>replacing them with '\0', and keeping a pointer to the next character
>at the same time.

>    - Kevin.

Thanks for the help


Fri, 04 Mar 2005 11:16:01 GMT  
 Need help with Simple C strings
On Mon, 16 Sep 2002 01:30:38 +0000 (UTC), Kevin Easton

Quote:

>> for loop, printf and fclose in roughly that order.  Other people may
>> object to strtok, and its true it can be dangerous, but it works.

Anything can be dangerous. I don't see that strtok() has any particular
propensity to cause problems.

Quote:
>I tend to use strchr to do that kind of parsing, finding the commas then
>replacing them with '\0', and keeping a pointer to the next character
>at the same time.

Which is pretty much what strtok() does in the simplest case. I use strtok()
often, particularly in cases where there may be multiple delimiters. Much nicer
than writing a one-off state machine every time. A common (if trivial) example
is a list of items separated by comma, space, tab, or any combination of those.


Sat, 05 Mar 2005 13:00:44 GMT  
 Need help with Simple C strings
On Tue, 17 Sep 2002 05:00:44 GMT, in comp.lang.c , Alan Balmer

Quote:

>On Mon, 16 Sep 2002 01:30:38 +0000 (UTC), Kevin Easton

>>> for loop, printf and fclose in roughly that order.  Other people may
>>> object to strtok, and its true it can be dangerous, but it works.

>Anything can be dangerous. I don't see that strtok() has any particular
>propensity to cause problems.

>>I tend to use strchr to do that kind of parsing, finding the commas then
>>replacing them with '\0', and keeping a pointer to the next character
>>at the same time.

strtok modifies the source string, and also requires error handling on
a per-item basis.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


Sun, 06 Mar 2005 06:37:09 GMT  
 Need help with Simple C strings


Quote:
>>>I tend to use strchr to do that kind of parsing, finding the commas then
>>>replacing them with '\0', and keeping a pointer to the next character
>>>at the same time.

>strtok modifies the source string, and also requires error handling on
>a per-item basis.

Of course it does. So does the strchr() approach outlined. What about it? I
assume the user can read and understand the man page. If not, he won't be
working for me, so I don't care.


Sun, 06 Mar 2005 12:42:35 GMT  
 Need help with Simple C strings

Quote:

> >Could someone please direct me on where I can find some help with
> >strings? I have an assignment (listed below) to complete and I'm still
> >a newbie with Simple C.

[how to process a fixed-format string]
[snip]

Quote:
> BTW if this were my project, I'd be looking at fopen, fgets, strtok, a
> for loop, printf and fclose in roughly that order.  Other people may
> object to strtok, and its true it can be dangerous, but it works.

I would suggest fgets and scanf since your string has a fixed format. If
you find that a particular string is invalid, you could reject it.

        david

--
If 91 were prime, it would be a counterexample to your conjecture.
    -- Bruce Wheeler



Mon, 07 Mar 2005 00:04:42 GMT  
 Need help with Simple C strings
On Wed, 18 Sep 2002 04:42:35 GMT, in comp.lang.c , Alan Balmer

Quote:



>>>>I tend to use strchr to do that kind of parsing, finding the commas then
>>>>replacing them with '\0', and keeping a pointer to the next character
>>>>at the same time.

>>strtok modifies the source string, and also requires error handling on
>>a per-item basis.

>Of course it does. So does the strchr() approach outlined. What about it?

sometimes thats undesirable, and your strchr approach is as bad. Other
more robust solutions can be found.

Quote:
>I assume the user can read and understand the man page. If not, he won't be working for me, so I don't care.

grin

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Mon, 07 Mar 2005 06:22:13 GMT  
 Need help with Simple C strings

Quote:

> On Wed, 18 Sep 2002 04:42:35 GMT, in comp.lang.c , Alan Balmer



>>>>>I tend to use strchr to do that kind of parsing, finding the commas then
>>>>>replacing them with '\0', and keeping a pointer to the next character
>>>>>at the same time.

>>>strtok modifies the source string, and also requires error handling on
>>>a per-item basis.

>>Of course it does. So does the strchr() approach outlined. What about it?

> sometimes thats undesirable, and your strchr approach is as bad. Other
> more robust solutions can be found.

Actually it was my approach, not his.  I wasn't intending it as a
criticism of your method at all, I just thought that I'd mention the way
I like to skin this particular cat.  (The main reason I don't use strtok
is simply because I don't like the way it makes use of static data, and
because accepting more than one deliminator character is almost always
overkill).

        - Kevin.



Mon, 07 Mar 2005 09:05:19 GMT  
 Need help with Simple C strings
On Thu, 19 Sep 2002 01:05:19 +0000 (UTC), Kevin Easton

Quote:

> (The main reason I don't use strtok
>is simply because I don't like the way it makes use of static data, and

Any parser uses state information of one kind or another, but I agree that
sometimes it's nice to have it visible. OTOH, we're talking about a standard
library function which any programmer should be able to handle without burning
himself, and it's not the only library function which does similar things.

Quote:
>because accepting more than one deliminator character is almost always
>overkill).

I disagree. In ancient days (yeah, I qualify as ancient in this business) we
wrote programs which made it easier for the computer. Now it's fashionable to
make programs easier for the user, even if the computer has to work harder.

Often, of course, there's a more compelling reason - the client's requirements
<g>.

Al Balmer
Balmer Consulting



Mon, 07 Mar 2005 12:23:56 GMT  
 Need help with Simple C strings

Quote:

> Actually it was my approach, not his.  I wasn't intending it as a
> criticism of your method at all, I just thought that I'd mention the way
> I like to skin this particular cat.  (The main reason I don't use strtok
> is simply because I don't like the way it makes use of static data, and
> because accepting more than one deliminator character is almost always
> overkill).

strtok() has at least these problems:

        * It merges adjacent delimiters.  If you use a comma as
          your delimiter, then "a,,b,c" is three tokens, not
          four.  This is often the wrong thing to do.  In fact,
          it is only the right thing to do, in my experience,
          when the delimiter set is limited to white space.

        * The identity of the delimiter is lost, because it is
          changed to a null terminator.

        * It modifies the string that it tokenizes.  This is bad
          because it forces you to make a copy of the string if
          you want to use it later.  It also means that you can't
          tokenize a string literal with it; this is not
          necessarily something you'd want to do all the time but
          it is surprising.

        * It can only be used once at a time.  If a sequence of
          strtok() calls is ongoing and another one is started,
          the state of the first one is lost.  This isn't a
          problem for small programs but it is easy to lose track
          of such things in hierarchies of nested functions in
          large programs.  In other words, strtok() breaks
          encapsulation.

--
"You call this a *C* question? What the hell are you smoking?" --Kaz



Mon, 07 Mar 2005 12:27:29 GMT  
 Need help with Simple C strings

Quote:

> (The main reason I don't use strtok is simply because I don't like
> the way it makes use of static data, and because accepting more
> than one deliminator character is almost always overkill).

I agree about strtok() and almost never use it, but I disagree quite
a bit about delimiters.  The most recent string tokenizer I wrote
was for splitting multiple filespecs, and I wanted the user to not
have to remember to use comma or semi-colon or whatever.  I wanted
the spec to gracefully allow whitespace, commas and/or semi-colons,
such that all four lines below are semantically identical:

    *.c *.cpp     *.hpp *.cpp
    *.c,*.cpp,*.hpp,*.cpp
    *.c, *.cpp, *.hpp, *.cpp
    *.c, *.cpp; *.hpp, *.cpp

Why do you consider that overkill?

--

|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL  |
|_____________________________________________|_______________________|



Tue, 08 Mar 2005 05:19:49 GMT  
 Need help with Simple C strings

Quote:

> In ancient days (yeah, I qualify as ancient in this business) we
> wrote programs which made it easier for the computer. Now it's
> fashionable to make programs easier for the user, even if the computer
> has to work harder.

As it should be.  A key criteria in my evaluation of software (my own
or others) is the extent to which it accommodates *me* and *my* way of
working.

--

|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL  |
|_____________________________________________|_______________________|



Tue, 08 Mar 2005 05:21:42 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. newbe/cs student, need help w/ code

2. newbie needs help to strings, strings, strings

3. Newbie: separate big .cs file into small .cs files

4. result of strstr(cs,ct) when ct is a null string

5. resx files needed for cs - files ??

6. Need C++ text for non cs major course

7. simple string manipulation...help?

8. help : beginners simple string question

9. URGENT HELP with a simple problem: reading file into string

10. Simple problem across processes - need simplest answer

11. Need some help in writing a simple C program

12. Need some help in writing a simple C program

 

 
Powered by phpBB® Forum Software