Please Help! Strings are confusing!

Discussion in 'C Programming' started by Sparticus, Dec 2, 2005.

  1. Sparticus

    Sparticus Guest

    It's been many years since i've programmed in c. I am trying to do
    somethign so simple, but it seems impossible. There has to be an
    easier way than what I am doing.

    What I want to do is build a string with user variables placed within
    it... for example :

    name = "Ryan";
    age = "28";

    mystring = "Hello, my name is " + name + " and I am " + age + " years
    old";
    (I know that won't compile, but I'm trying to find a way to do
    something like that)

    So when this is run, the variable mystring would contain :

    "Hello, my name is Ryan and I am 28 years old"

    How do I do this?

    This is how I am doing it so far and there has to be a better way :

    strcpy(mystring,"Hello, my name is ");
    strcat(mystring,name);
    strcat(mystring,"and I am ");
    strcat(mystring,age);
    strcat(mystring,"years old);

    Thanx a bunch for the help!!

    Ryan Ritten
     
    Sparticus, Dec 2, 2005
    #1
    1. Advertising

  2. Sparticus

    Markus Moll Guest

    Hi

    Sparticus wrote:

    > What I want to do is build a string with user variables placed within
    > it...

    [...]
    > How do I do this?
    >
    > This is how I am doing it so far and there has to be a better way :

    [Approach with lots of strcats]

    Yes, there is indeed a better way: Use sprintf!
    It works just like printf, but prints to a string...

    Markus
     
    Markus Moll, Dec 2, 2005
    #2
    1. Advertising

  3. Sparticus

    pete Guest

    Sparticus wrote:
    >
    > It's been many years since i've programmed in c. I am trying to do
    > somethign so simple, but it seems impossible. There has to be an
    > easier way than what I am doing.
    >
    > What I want to do is build a string with user variables placed within
    > it... for example :
    >
    > name = "Ryan";
    > age = "28";
    >
    > mystring = "Hello, my name is " + name + " and I am " + age + " years
    > old";
    > (I know that won't compile, but I'm trying to find a way to do
    > something like that)
    >
    > So when this is run, the variable mystring would contain :
    >
    > "Hello, my name is Ryan and I am 28 years old"
    >
    > How do I do this?
    >
    > This is how I am doing it so far and there has to be a better way :
    >
    > strcpy(mystring,"Hello, my name is ");
    > strcat(mystring,name);
    > strcat(mystring,"and I am ");
    > strcat(mystring,age);
    > strcat(mystring,"years old);
    >
    > Thanx a bunch for the help!!


    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    char name[] = "Ryan";
    char age[] = "28";
    char mystring
    [sizeof"Hello, my name is Ryan and I am 28 years old"];

    sprintf(mystring,
    "Hello, my name is %s and I am %s years old", name, age);
    puts(mystring);
    return 0;
    }

    /* END new.c */


    --
    pete
     
    pete, Dec 2, 2005
    #3
  4. Sparticus

    Berryo Guest

    Re: Please Help! Strings are confusing!

    Sparticus wrote:
    > It's been many years since i've programmed in c. I am trying to do
    > somethign so simple, but it seems impossible. There has to be an
    > easier way than what I am doing.
    >
    > What I want to do is build a string with user variables placed within
    > it... for example :
    >
    > name = "Ryan";
    > age = "28";
    >
    > mystring = "Hello, my name is " + name + " and I am " + age + " years
    > old";
    > (I know that won't compile, but I'm trying to find a way to do
    > something like that)
    >
    > So when this is run, the variable mystring would contain :
    >
    > "Hello, my name is Ryan and I am 28 years old"
    >
    > How do I do this?
    >
    > This is how I am doing it so far and there has to be a better way :
    >
    > strcpy(mystring,"Hello, my name is ");
    > strcat(mystring,name);
    > strcat(mystring,"and I am ");
    > strcat(mystring,age);
    > strcat(mystring,"years old);
    >
    > Thanx a bunch for the help!!
    >
    > Ryan Ritten


    That's as good a way as any, so long as mystring is a big enough buffer
    to hold the final result. Another option would be sprintf:

    sprintf(mystring,"Hello, my name is %s and I am %s years old", name,
    age) ;
     
    Berryo, Dec 2, 2005
    #4
  5. Sparticus

    Sparticus Guest

    Re: Please Help! Strings are confusing!

    thanx guys. that's exactly what I was looking for :)
     
    Sparticus, Dec 2, 2005
    #5
  6. Re: Please Help! Strings are confusing!

    Berryo <> wrote:

    > That's as good a way as any, so long as mystring is a big enough buffer
    > to hold the final result. Another option would be sprintf:


    OP may find the C99 function snprintf() useful as well. It's
    fairly widely available even on implementations that do not conform to
    C99.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 2, 2005
    #6
  7. Sparticus

    Simon Biber Guest

    Re: Please Help! Strings are confusing!

    Christopher Benson-Manica wrote:
    > Berryo <> wrote:
    >
    >
    >>That's as good a way as any, so long as mystring is a big enough buffer
    >>to hold the final result. Another option would be sprintf:

    >
    >
    > OP may find the C99 function snprintf() useful as well. It's
    > fairly widely available even on implementations that do not conform to
    > C99.


    Unfortunately, on the implementations that do not conform to C99, the
    snprintf function has quite different behaviour. Don't rely on the C99
    specification and expect it to work that way everywhere. It is possible
    to code around these old implementations, but it's tricky.

    --
    Simon.
     
    Simon Biber, Dec 6, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    541
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    646
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,785
    Mike Hewson
    Jan 7, 2005
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    787
    Malcolm
    Jun 24, 2006
  5. Luna Moon

    help! confusing "const"?

    Luna Moon, Jul 6, 2008, in forum: C++
    Replies:
    5
    Views:
    321
    James Kanze
    Jul 7, 2008
Loading...

Share This Page