standard operations about string operation

S

s88

Howdy everyone:
for example, the psudo code is
string ABC;
string DEF;
string cat = ABC+"__"+DEF;
what is the most popular operations in C to make the above psudo code?
for example, I'll do it like that
char *ABC="THIS IS ABC";
char *DEF="THIS IS DEF";
int size = sizeof ABC + sizeof DEF + sizeof "__";
char *new_str = (char *)malloc(size);
strcpy(new_str,ABC);
strcat(new_str,"__");
strcat(new_str,DEF);
...
Does anyone have better method?
Thanx!
 
K

Keith Thompson

s88 said:
Howdy everyone:
for example, the psudo code is
string ABC;
string DEF;
string cat = ABC+"__"+DEF;
what is the most popular operations in C to make the above psudo code?
for example, I'll do it like that
char *ABC="THIS IS ABC";
char *DEF="THIS IS DEF";
int size = sizeof ABC + sizeof DEF + sizeof "__";
char *new_str = (char *)malloc(size);
strcpy(new_str,ABC);
strcat(new_str,"__");
strcat(new_str,DEF);
...
Does anyone have better method?

sizeof ABC gives you the size of the pointer, not the length of the
string. You also have to allow for the trailing '\0' that marks the
end of the string. (And all-caps names are conventionally used for
macros, not for variables.)

char *abc = "THIS IS ABC";
char *def = "THIS IS DEF";
size_t size = strlen(abc) + strlen("__") + strlen(def) + 1;
char *new_str = malloc(size); /* Note: No cast */
strcpy(new_str, abc);
strcat(new_str, "__");
strcat(new_str, def);

Or the last three lines could be replaced by:

sprintf(new_str, "%s%s%s", abc, "__", def);

but using sprintf is likely to impose extra overhead.

The strlen("__") is arguably inefficient, since the length is known at
compilation time, but this issue goes away (read on).

The multiple occurrences of "__" are a potential problem, since it's
difficult to keep them all in sync. I'd declare another pointer
variable:

char *underscores = "__";

which can then be treated the same way as abc and def.

Here's a function that encapsulates the whole thing:

/*
* Returns a pointer to a string consisting of s1, s2, and s3
* catenated together. The caller is responsible for free()ing
* the allocated memory.
*/
char *cat3(char *s1, char *s2, char *s3)
{
char *result = malloc(strlen(s1) + strlen(s2) + strlen(s3) + 1);
strcpy(result, s1);
strcat(result, s2);
strcat(result, s3);
return result;
}

...
char *abc = "THIS IS ABC";
char *def = "THIS IS DEF";
char *new_str = cat3(abc, "__", def);
 
J

Jens.Toerring

s88 said:
Howdy everyone:
for example, the psudo code is
string ABC;
string DEF;
string cat = ABC+"__"+DEF;
what is the most popular operations in C to make the above psudo code?
for example, I'll do it like that
char *ABC="THIS IS ABC";
char *DEF="THIS IS DEF";
int size = sizeof ABC + sizeof DEF + sizeof "__";

First of all, sizeof won't tell you the length of the string
when you use it on a char pointer. It tells you how much memory
youu need for the object it's applied to. Since both 'ABC'and
'DEF' are char pointers you will not get the length of the strings
they point to but how much space each pointer needs. The only
correct use of sizeof here is that for the string literal. Also,
the result of sizeof is size_t why don't you use that? Moreover,
that's the argument type malloc() expects.
char *new_str = (char *)malloc(size);

Since the way you use sizeof to determine the amount of memory
doesn't work you need something like e.g.

char *new_str = malloc( strlen( ABC ) + strlen( DEF )
+ sizeof "__" );

(The 'sizeof "__"' will return 3 since it also counts the
trailing '\0', so you end up with enough space for the
final string, including space for the '\0' at the end).
There's probably no good reason to cast the return value of
malloc() unless you try to compile this with a C++ compiler.
All else you get from this is keeping the compiler from com-
plaining if you forget to include <stdlib.h>. And, finally,
you should of course check the return value of malloc() for
failure before using what 'new_str' points to.
strcpy(new_str,ABC);
strcat(new_str,"__");
strcat(new_str,DEF);
...
Does anyone have better method?

That's one method. Another one would be e.g.

sprintf( new_str, "%s__%s", ABC, DEF );

or variations of that. What's better depends a lot on what you
mean by "better". Execution speed, readability of code or some-
thing else?
Regards, Jens
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top