473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

String appending

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.

Nov 15 '05 #1
9 1612
Selvesteen wrote:
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?

Nov 15 '05 #2
Suman:
Selvesteen: ....
for eg : I need to append "SSL"_ before { CLASS, SEED, RANDOM } to get
result as SSL_CLASS, SSL_SEED, SSL_RANDOM ...

.... /*--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
Nov 15 '05 #3


Jirka Klaue wrote:
Suman:
Selvesteen:

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

...
/*--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.

Which one ?

Nov 15 '05 #4


Suman wrote:
Jirka Klaue wrote:
Suman:
Selvesteen:

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

...
/*--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.

Which one ?

Ah! ha. Hubris! Got it.

Nov 15 '05 #5
Suman:
Jirka Klaue:
Suman: ....
> /*--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.

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
Nov 15 '05 #6


Selvesteen wrote:
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[i], "%s_%s", prefix, names[i]);
printf("%s\n", results[i]);
}

return 0;
}

Nov 15 '05 #7


Jirka Klaue wrote:
Suman:
Jirka Klaue:
Suman:
...

[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.

Nov 15 '05 #8
On Fri, 1 Jul 2005 08:46:24 -0500, jo*******@my-deja.com wrote
(in article
<11*********************@z14g2000cwz.googlegroups. com>):
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[i], "%s_%s", prefix, names[i]);
Delete this line: printf("%s\n", results[i]);
Add this line:
printf("%s_%s\n", prefix, names[i]); }

return 0;
}


--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #9


Randy Howard wrote:
On Fri, 1 Jul 2005 08:46:24 -0500, jo*******@my-deja.com wrote
(in article
<11*********************@z14g2000cwz.googlegroups. com>):
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.

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
2
by: Karuppasamy | last post by:
Hi I am using a variable of StringBuilder to form a string. After appending a string of value like this {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Verdana;}}
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
1
by: mikemac76 | last post by:
I am trying to build a test harness to practice appending strings to an rtf string. I am doing this to simulate the string I'll be sending over the wire and receiving on the other end of an IM...
13
by: diffuser78 | last post by:
I want to print number 0 to 9 in one line like this 0 1 2 3 4 5 6 7 8 9 if I do like this, it prints in different lines for i in xrange(10): print i so i tried like this
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
7
by: simonZ | last post by:
I have array variable transfer: String transfer which has some data Than I would like to convert this data into string: Which way is more efficient: StringBuilder rezult=new StringBuilder(); ...
4
by: mthread | last post by:
Hi, I am using a string variable in which I do lot of appending. The only difficulty I am facing as of now is appending a integer/float value to this variable. Although I can accomplish this task...
0
by: Bart Kastermans | last post by:
|    def __str__ (self): I did some timing of operations involved. Doing this I found that the operation below to get a string representation for trees was in fact not quadratic. The final...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.