String Concatenation

String Concatenation

Post by Kevin Diggin » Sat, 08 Jan 2000 04:00:00



*** In BASIC ***

A$ = "Hello "
B$ = "World"
A$ =  A$ + B$
Print A$

Result ..... Hello World

*** In C ***

#include <stdio.h>
static  char   A [20];
static  char   B [20];
int main()
{
sprintf(A,"%s","Hello ");
sprintf(B,"%s","World");
sprintf(A,"%s%s",A,B);
printf("%s\n",B);
return 0;

Quote:}

Result ..... World

Obviously, I should not be using sprintf to accomplish this approach.
Could someone kindly lift the fog and show me the correct method.

Thanks ...

 
 
 

String Concatenation

Post by Herma » Sun, 09 Jan 2000 04:00:00


Hi there,

Use strcat() or strncat() as in

char    string[256] = "hello";
char    stringtoo[256] = "world";

strcat(string, " world");
or
strcat(string, stringtoo);

printf("%s", string);

Cheers,

Herman
http://www.AerospaceSoftware.com


> *** In BASIC ***

> A$ = "Hello "
> B$ = "World"
> A$ =  A$ + B$
> Print A$

> Result ..... Hello World

> *** In C ***

> #include <stdio.h>
> static  char   A [20];
> static  char   B [20];
> int main()
> {
> sprintf(A,"%s","Hello ");
> sprintf(B,"%s","World");
> sprintf(A,"%s%s",A,B);
> printf("%s\n",B);
> return 0;
> }

> Result ..... World

> Obviously, I should not be using sprintf to accomplish this approach.
> Could someone kindly lift the fog and show me the correct method.

> Thanks ...


 
 
 

String Concatenation

Post by Tobias Schaef » Sun, 09 Jan 2000 04:00:00


Hi,

I played with your problem and found the solution:

The Problem:

Quote:>#include <stdio.h>
>static  char   A [20];
>static  char   B [20];
>int main()
>{
>sprintf(A,"%s","Hello ");
>sprintf(B,"%s","World");
>sprintf(A,"%s%s",A,B);
>printf("%s\n",B);
>return 0;
>}
>Result ..... World

The Problem is that "sprintf" empties the output variable,
when it is started. Therefor A is empty, when it should be
written in the output string (which is also A). Therefore:
No A, only B. No "Hello", only "World".

Basic doesn't have this problem, because it utilizes intermediate
string.

The Solution :

#include <stdio.h>
static  char   A [20];
static  char   B [20];
static  char   C [20];
int main()
{
sprintf(A,"%s","Hello ");
sprintf(B,"%s","World");
sprintf(C,"%s%s",A,B);
printf("%s\n",C);
return 0;

Quote:}
>Obviously, I should not be using sprintf to accomplish this approach.

Why not ...
Every problem can be solved in a wide variety of ways.
Reminds me of ... umm ... have a look :

http://www.sadinoff.com/fun/quotes/programmers.txt

Quote:>Could someone kindly lift the fog and show me the correct method.

grFogMode(GR_FOG_DISABLE);

Toby

 
 
 

String Concatenation

Post by Kevin Diggin » Sun, 09 Jan 2000 04:00:00


Thanks Tobias.  You explained the problem very well
and illustrated the use of the third variable which is the
same solution I arrived at.  I could have used the strcat
and rewritten it that way but that was not the point.

I liked your link showing the art of obfuscation and ridiculous
complexity.  I laughed ... then cried, because it was true.


> Hi,

> I played with your problem and found the solution:

> The Problem:

> >#include <stdio.h>
> >static  char   A [20];
> >static  char   B [20];
> >int main()
> >{
> >sprintf(A,"%s","Hello ");
> >sprintf(B,"%s","World");
> >sprintf(A,"%s%s",A,B);
> >printf("%s\n",B);
> >return 0;
> >}

> >Result ..... World

> The Problem is that "sprintf" empties the output variable,
> when it is started. Therefor A is empty, when it should be
> written in the output string (which is also A). Therefore:
> No A, only B. No "Hello", only "World".

> Basic doesn't have this problem, because it utilizes intermediate
> string.

> The Solution :

> #include <stdio.h>
> static  char   A [20];
> static  char   B [20];
> static  char   C [20];
> int main()
> {
> sprintf(A,"%s","Hello ");
> sprintf(B,"%s","World");
> sprintf(C,"%s%s",A,B);
> printf("%s\n",C);
> return 0;
> }

> >Obviously, I should not be using sprintf to accomplish this approach.

> Why not ...
> Every problem can be solved in a wide variety of ways.
> Reminds me of ... umm ... have a look :

> http://www.sadinoff.com/fun/quotes/programmers.txt

> >Could someone kindly lift the fog and show me the correct method.

> grFogMode(GR_FOG_DISABLE);

> Toby

 
 
 

1. string concatenation

Looks like your coming from a BASIC background... nothing wrong with that. C
handles strings differently. First of all you only have a pointer to a string,
no real space allocated for it. Try something like this (abbreviated version of
your example):

#include <stdio.h>

char a='a';
char b='b';
char c='c';

char text[4]; /* space for characters and null */

void main(void)
{
  sprintf(text, "%c%c%c", a, b, c);
  printf("%s\n", text);

Hope this helps!
Joe

2. Robot Auto Racing Results

3. String concatenation with spss v9

4. Hey, COOL!!!!

5. string concatenation

6. Windows/DSL problem

7. a problem with string concatenation

8. Converting to unicode using c#

9. String concatenation in VT 4.0

10. String Concatenation

11. trying to repeat string concatenation within a range(1-2-3 Millenium)

12. String Concatenation

13. Functional index and string concatenation