String appending

S

Selvesteen

Hello Gurus,

I need to append a sub-string at the start of strings using C. What is
the best way to achieve that?

for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...

Advance thanks for all your help.
 
S

Suman

Selvesteen said:
Hello Gurus,

I need to append a sub-string at the start of strings using C. What is
the best way to achieve that?

for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...

Advance thanks for all your help.
<from code posted earlier, by W Roberson, in recent past,
some other thread, "#defines and strings" ...>

/*--BEGIN--*/

#include <stdio.h>

#define PASTE(T1,T2) T1##T2
#define Append(N) (N, PASTE("SSL_",N))

int main()
{
printf("%s\n", Append("CLASS"));
return 0;
}
/*--END--*/

Does that help?
 
J

Jirka Klaue

Suman:
Selvesteen: .... ....
/*--BEGIN--*/

#include <stdio.h>

#define PASTE(T1,T2) T1##T2
#define Append(N) (N, PASTE("SSL_",N))

int main()
{
printf("%s\n", Append("CLASS"));
return 0;
}
/*--END--*/

Does that help?

Most probably not. This is not valid C and shouldn't compile.
And what is the 'N,' in Append(N) for?

#include <stdio.h>

#define APPEND(x) ("SSL_" #x)

int main()
{
printf("%s\n", APPEND(CLASS));
return 0;
}

Jirka
 
J

Jirka Klaue

Suman:
Jirka Klaue:
Which one ?

PASTE("A", "B") yields "A""B" which is not a valid preprocessing token.

Do you use Microsoft's C-Compiler by any chance?

Jirka
 
J

john_bode

Selvesteen said:
Hello Gurus,

I need to append a sub-string at the start of strings using C. What is
the best way to achieve that?

for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...

Advance thanks for all your help.

Slightly different approach from using macros:

#include <stdio.h>

#define MAX_NAME_LEN 10 /* length of longest result */
/* string, not counting */
/* terminator */
#define MAX_NAME_COUNT 3

int main(void)
{
char *prefix="SSL";
char *names[MAX_NAME_COUNT]={"CLASS", "SEED", "RANDOM"};
char results[MAX_NAME_COUNT][MAX_RESULT_LEN+1];
int i;

for (i = 0; i < MAX_NAME_COUNT; i++)
{
sprintf(results, "%s_%s", prefix, names);
printf("%s\n", results);
}

return 0;
}
 
S

Suman

Jirka said:
Suman:
Jirka Klaue: ...
[snipped]

PASTE("A", "B") yields "A""B" which is not a valid preprocessing token.

Do you use Microsoft's C-Compiler by any chance?
Not if my manager's not around :)
But this time, you've hit bull's eye! I did, and ... I apologise for
causing (any/all) confusion for that code.

I checked with gcc and it bit me hard.
 
R

Randy Howard

Slightly different approach from using macros:

#include <stdio.h>

#define MAX_NAME_LEN 10 /* length of longest result */
/* string, not counting */
/* terminator */
#define MAX_NAME_COUNT 3

int main(void)
{
char *prefix="SSL";
char *names[MAX_NAME_COUNT]={"CLASS", "SEED", "RANDOM"};

Delete this line:
char results[MAX_NAME_COUNT][MAX_RESULT_LEN+1];
int i;

for (i = 0; i < MAX_NAME_COUNT; i++)
{ Delete this line:
sprintf(results, "%s_%s", prefix, names);


Delete this line:
printf("%s\n", results);


Add this line:
printf("%s_%s\n", prefix, names);
 
J

john_bode

Randy said:
Slightly different approach from using macros:

#include <stdio.h>

#define MAX_NAME_LEN 10 /* length of longest result */
/* string, not counting */
/* terminator */
#define MAX_NAME_COUNT 3

int main(void)
{
char *prefix="SSL";
char *names[MAX_NAME_COUNT]={"CLASS", "SEED", "RANDOM"};

Delete this line:
char results[MAX_NAME_COUNT][MAX_RESULT_LEN+1];

[Remaining helpful hints snipped]

I thought it *might* be useful to the OP to demonstrate how to keep the
result strings around and save a few cycles later on.

Obviously, I was wrong. In the future I will endeavor to never suggest
anything so old-fashioned and stupid as keeping the results of your
operations around for later use.
 

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

Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top