string question

I have 2 strings

int min = 2;
int max = 100;

char *p = "range is %d to %d";
char* q = "min,max";

Now i need to have a string "range is 2 to 100";

how to achieve this using p and q.remeber that i need to collect the resulting string into a buffer.

Regards,
Vamshi

Comments

  • see the documentation of [link=http://cplusplus.com/reference/clibrary/cstdio/sprintf.html]sprintf()[/link].
    [hr][purple]~Donotalo()[/purple]
  • [code]char buf[32]={0};
    sprintf (buf, "range is %d to %d", min, max);[/code]

    buf now contains your string.

    *edit: Too slow! :)

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • : [code]: char buf[32]={0};
    : sprintf (buf, "range is %d to %d", min, max);[/code]:
    :
    : buf now contains your string.
    :
    : *edit: Too slow! :)
    :
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.brokenthorn.com]Website ::
    : OS Development Series[rightbr][/link][/size]
    :

    Ok let me make myself more clear.
    I have a function something like this:

    char* printArgs(char* format,char* args)
    {
    ...
    }

    format may contain "range is %d to %d" and args "min,max" .
    Now i need to write the code generically in the function printArgs so that first %d is replaced by min value and second %d with max.

    Also note that format can be any valid c string with %d,%s,%c etc and there can be any number of arguments in the args string.

    Regards,
    Vamshi
  • when there are generic functions like sprintf() available at hand, it is unclear to me why you need to define your own generic function! To write your own printArgs function, you need to parse format and args, and replace corresponding specifiers (%d, %s etc). This is just my initial idea. to extract a value from a string, [link=http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html]sscanf()[/link] may handy.
    [hr][purple]~Donotalo()[/purple]
  • ...Unless you are looking at using variable argument lists?
    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories