Best way of returning a substring form a string

Posted on 2004-11-17
Last Modified: 2010-05-18
Hi all,

I currently have a function named readLine(char* buffer, char* pointValue, int point) with buffer being the entire line value of a file, pointValue being the substring I need to return and point being the value to read. Now I just wanted to know what would be the best way of finding that substring in the buffer ? scanf inside a switch?

buffer= -204.88    -648.67    -560.46
pointValue= should be -648.67 without spaces

Thanks for your help,

Question by:The_Kingpin08
    LVL 16

    Expert Comment

    You'd probably be better to use sscanf inside a loop. The loop would count down 'point' while the scanf function 'eats' one number at a time. Use %n to deposit how many characters tha scanf has read so you know where to start each time around the loop.

    You may prefer to find spaces in the line instead of using scanf for the numbers.

    Please make an attempt at the code and post what you've tried and what goes wrong. We have to be on the lookout for 'Homework'.

    LVL 55

    Expert Comment

    by:Jaime Olivares
    Is really necessary to read a number at a time, why don't you read all numbers at a time and store values in a numeric variable?

    buffer= -204.88    -648.67    -560.46
    pointValue= should be -648.67 without spaces

    float a,b,c;
    sscanf(buffer, "%f %f %f", &a, &b, &c);
    LVL 45

    Expert Comment

    Hi Frank,

    May be strchr if you have fixed number of spaces (say a single space) or strtok if you do not mind the beffer being modified. Both are basically the same approach as Paul highligted "eat one number at a time".

    If your line format is fixed, i.e. every field is fixed width and line length is fixed, then you can simply maintain a table to get you the corresponding point value index in the array of chars.

    In short, we need to know more about your data ...

    Can it be modified ... if yes then strtok may be the easiest to read and cleanest to code way

    If no, then are number of spaces fixed? If yes, how much?

    Using scanf inside a loop or copying the string should be *avoided* and avoiding them will give you faster code.


    Author Comment

    Hi sunnycoder,

    the line format isn't fixed because there can be x spaces before reachng the data and x spaces between each number. The returned value format isn't fixed since we can have -2.12 or 14.92. However, there will always be 2 digitts after the '.'

    At this point I don't think there would be any problem modifying the buffer, as long as it returns all the values. Any example of a strtok use ?

    Alsi I have a struct point declared as

    typedef struct point* PtrPoint;
    typedef struct point
          float x;
          float y;
          float z;

    could I directly pass the value of PtrPoint to the function or it will be better that the function takes a variable and then assign it to the pointer ?

    Thanks for the support,

    LVL 45

    Accepted Solution

    Hi Frank,

    >could I directly pass the value of PtrPoint to the function or it will be better that the function takes a variable and then assign
    >it to the pointer ?
    It is always cleaner if the calling function passes allocated memory to called function. That way called function does not have to bother about freeing up the memory or memory going out of scope. So I recommend the former.

    >Any example of a strtok use ?

    Also refer to the man page

    If you face any problems using strtok, post back.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now