Because C doesn't include strings as a basic data type, it doesn't have many
of the nice string-handling features of Basic (or C++ or Java or Perl or
...). Remember that a C-style "string" is really an array of characters, and
a string pointer points to the location of array element 0 (zero).

Here are some ideas that might work for you:

1. To find the first space, use brute force. Something like:
        for ( i = 0 ; i < strlen(teststring) ; i++ )
                {
                if ( teststring[i] == " " )
                         break  ;
                }       ;

This will leave i with the relative location of the first space or, if there
are no spaces in the string, with the relative location of the last byte of
the string.

2. To extract the space-delimited substrings, use strtok() to "tokenize" the
string. The man page should get you started; if not, ask again and I'll try
to dig up a sample snippet of code.

3. Pointers are just numbers, so to get the relative position of the space
in the string using strstr, you could just subtract the result from the
value of the main pointer (casting as needed). I haven't actually done this,
so I can't give you an exact example of how to do it, but it should be
possible (if you don't like the above brute-force answer).

4. To extract the string,, since you know its starting point and its length,
you could use the pointer returned by strstr in a call to strncpy(). Again,
the man page should get you started - if you need more, ask again.

BTW, linux-newbie doesn't seem like the best place to pose this question ...
but I don't know where would be. Does anyone else have a suggestion?

At 05:51 PM 6/30/99 EDT, Michael B Golden wrote:
>       I was trying to work on a C program, but I can't figure out how
>to do some
>things. What I am looking for is a command in C similar to the mid$
>command in Visual Basic. What I need to do is take a string out of
>another
>string. I need to be able to specify the start point, and then end point.
>I also need to find a function that can look through the string, and find
>the first space (beginning where I tell it to, not the beginning), and
>return the value as an integer. Here is a sample line of input and what I
>need it to output:
>
>Input: 0 Inbox FOLD0000.FRM 443 442 1 1
>Needed Output: Inbox
>
>Does anyone know how to do this? I tried using the strstr() function to
>find the space, but I couldn't figure out how to work with the pointer it
>outputs. 
>
>And Lawson, do you know what the beginning 0 and the trailing 3 numbers
>are? I know what the first one is.

------------------------------------"Never tell me the odds!"---
Ray Olszewski                                        -- Han Solo
Palo Alto, CA  94303-3603                       [EMAIL PROTECTED]        
----------------------------------------------------------------

Reply via email to