Please Help! Strings are confusing!

S

Sparticus

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
 
M

Markus Moll

Hi
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
 
P

pete

Sparticus said:
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 */
 
B

Berryo

Sparticus said:
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) ;
 
C

Christopher Benson-Manica

Berryo said:
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.
 
S

Simon Biber

Christopher said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

I need help 8
Please critique my code for fun learning project. 5
Help please 8
Please help me!!! 3
Please help 7
Code help please 4
Please help me to solve this JS problem 6
I dont get this. Please help me!! 2

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top