Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help me with Concatenating strings

Reply
Thread Tools

Help me with Concatenating strings

 
 
c
Guest
Posts: n/a
 
      09-24-2006
Hi everybody.

I'm working on converting a program wriiten on perl to C, and facing a
problem with concatenate strings.
Now here is a small program that descripe the problem, if you help me
to solve in this small code, I can solve it on my own program...you
don't want to have head-ache

So, the problem excatly is, I built an array..just like this one.

char *my_array [10];
my_array[0] = "Sally\n";
my_array[1] = "Ndiya\n";
my_array[2] = "Samantha\n";
my_array[3] = "Sara\n";
my_array[4] = "Cadillac\n";
my_array[5] = "GM For Ever\n";
my_array[6] = "SlacWare\n";
my_array[7] = "Google\n";
my_array[8] = "Google more and more\n";
my_array[9] = "You\n";
my_array[10] = "Computers\n";

my program will recieve an argument -char *argv[]- from the user.

int main(int argc,char *argv[])


I want to concatenate the given argument to at the first of each line
of my array.
so, if we imagine the array after the concatenating, it would look like
this (suppose the given argument is "ILove").

my_array[0] = "ILoveSally\n";
my_array[1] = "ILoveNdiya\n";
my_array[2] = "ILovesamantha\n";
my_array[3] = "ILoveSara\n";
my_array[4] = "ILoveCadillac\n";
my_array[5] = "ILoveGM For Ever\n";
my_array[6] = "ILoveSlacWare\n";
my_array[7] = "ILoveGoogle\n";
my_array[8] = "ILoveGoogle again\n";
my_array[9] = "ILoveSAMI\n";
my_array[10] = "ILoveComputers\n";*/

I tried strcat() -strings.h- but the result wasn't what I'm looking
for.

read the code..and the comments will help to understand the
problem..here is the whole code of my problem example.

#include <stdio.h>

int main(int argc,char *argv[])
{int i=-1;
char *my_array [10];

my_array[0] = "Sally\n";
my_array[1] = "Ndiya\n";
my_array[2] = "Samantha\n";
my_array[3] = "Sara\n";
my_array[4] = "Cadillac\n";
my_array[5] = "GM For Ever\n";
my_array[6] = "SlacWare\n";
my_array[7] = "Google\n";
my_array[8] = "Google more and more\n";
my_array[9] = "You\n";
my_array[10] = "Computers\n";

while(i++ < 10)
{
printf(argv[1]); //Don't do it like this..read my comments please.
printf(my_array[i]);
}

// I need your help to fix below loop, I want to cancatenate the given
data -*argv[]- to each line of my array.
// For Example, if the user enetered
// root@localhost# test ILove
// the result would be:
// ILoveSally
// ILoveNdiya
// ..etc
//
//But don't do it by printting the given data followed by my_array
lines(I mean by printting it twoice), because I need the given data to
be concatenated at the first of each line of my array.
//So, my_array would look like this
// my_array[0] = "ILoveSally\n";
// my_array[1] = "ILoveNdiya\n";
// my_array[2] = "ILovesamantha\n";
// my_array[3] = "ILoveSara\n";
// my_array[4] = "ILoveCadillac\n";
// my_array[5] = "ILoveGM For Ever\n";
// my_array[6] = "ILoveSlacWare\n";
// my_array[7] = "ILoveGoogle\n";
// my_array[8] = "ILoveGoogle again\n";
// my_array[9] = "ILoveSAMI\n";
// my_array[10] = "ILoveComputers\n";*/

while (i++ < 10)
{
// type your code here..Thanks
}


return 0;
}


thank you for your time.

 
Reply With Quote
 
 
 
 
Ancient_Hacker
Guest
Posts: n/a
 
      09-24-2006
you can't change anything in that array, as those are all read-only
constants.

So you either need another array, either fixed size strings, or get the
memory dynamically, either thru malloc() or out of your own array. I
recommend something aloong the lines of the latter for simplicity. Not
tested, but generally on track below code is. Add some obvious error
checks for extra credit. Use snprintf for extra safety.


char BigPool[ 1000000 ]; // or whatever size you expect;

char Strs[ STRINGS_IN_LIST ] = { "foo", "bar", ........ };
int Free = 0;

int i, Len;

for( i = 0; i < STRINGS_IN_LIST; i++ )
Free += sprintf( BigPool[ Free ], "%s%s", argv[ whatever ], Strs[
i ] ) + 1;



.

 
Reply With Quote
 
 
 
 
Ancient_Hacker
Guest
Posts: n/a
 
      09-24-2006
oops, I forgot to log the address of each created string, try this:


char BigPool[ 1000000 ]; // or whatever size you expect;

char Strs[ STRINGS_IN_LIST ] = { "foo", "bar", ........ };

char * OutStrs[ STRINGS_IN_LIST ];


int Free = 0;

int i, Len;

for( i = 0; i < STRINGS_IN_LIST; i++ )
Free += sprintf( OutStrs[ i ] = BigPool[ Free ], "%s%s", argv[
whatever ], Strs[ i ] ) + 1;



.

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-24-2006
c posted:

> char *my_array [10];



This array contains ten elements.


> my_array[0] = "Sally\n";
> my_array[1] = "Ndiya\n";
> my_array[2] = "Samantha\n";
> my_array[3] = "Sara\n";
> my_array[4] = "Cadillac\n";
> my_array[5] = "GM For Ever\n";
> my_array[6] = "SlacWare\n";
> my_array[7] = "Google\n";
> my_array[8] = "Google more and more\n";
> my_array[9] = "You\n";
> my_array[10] = "Computers\n";



Here, you assign to eleven separate elements.


> #include <stdio.h>
>
> int main(int argc,char *argv[])
> {int i=-1;
> char *my_array [10];
>
> my_array[0] = "Sally\n";
> my_array[1] = "Ndiya\n";
> my_array[2] = "Samantha\n";
> my_array[3] = "Sara\n";
> my_array[4] = "Cadillac\n";
> my_array[5] = "GM For Ever\n";
> my_array[6] = "SlacWare\n";
> my_array[7] = "Google\n";
> my_array[8] = "Google more and more\n";
> my_array[9] = "You\n";
> my_array[10] = "Computers\n";



Again here, you assign to eleven elements.


> while(i++ < 10)
> {
> printf(argv[1]); //Don't do it like this..read my comments please.
> printf(my_array[i]);
> }



Why resort to that? Looping 0 through 10 can be much simpler:

unsigned i = 0;

do {

}while(++i != 11);

Or, if you'd prefer the less efficient simplicity of:

for(unsigned i = 0; i != 11; ++i)
{

}

You're still pretending that you're array has eleven elements though...

--

Frederick Gotham
 
Reply With Quote
 
c
Guest
Posts: n/a
 
      09-24-2006
Thank you, but this is just a peace of code i don't care about it that
much.

Frederick Gotham wrote:
> c posted:
>
> > char *my_array [10];

>
>
> This array contains ten elements.
>
>
> > my_array[0] = "Sally\n";
> > my_array[1] = "Ndiya\n";
> > my_array[2] = "Samantha\n";
> > my_array[3] = "Sara\n";
> > my_array[4] = "Cadillac\n";
> > my_array[5] = "GM For Ever\n";
> > my_array[6] = "SlacWare\n";
> > my_array[7] = "Google\n";
> > my_array[8] = "Google more and more\n";
> > my_array[9] = "You\n";
> > my_array[10] = "Computers\n";

>
>
> Here, you assign to eleven separate elements.
>
>
> > #include <stdio.h>
> >
> > int main(int argc,char *argv[])
> > {int i=-1;
> > char *my_array [10];
> >
> > my_array[0] = "Sally\n";
> > my_array[1] = "Ndiya\n";
> > my_array[2] = "Samantha\n";
> > my_array[3] = "Sara\n";
> > my_array[4] = "Cadillac\n";
> > my_array[5] = "GM For Ever\n";
> > my_array[6] = "SlacWare\n";
> > my_array[7] = "Google\n";
> > my_array[8] = "Google more and more\n";
> > my_array[9] = "You\n";
> > my_array[10] = "Computers\n";

>
>
> Again here, you assign to eleven elements.
>
>
> > while(i++ < 10)
> > {
> > printf(argv[1]); //Don't do it like this..read my comments please.
> > printf(my_array[i]);
> > }

>
>
> Why resort to that? Looping 0 through 10 can be much simpler:
>
> unsigned i = 0;
>
> do {
>
> }while(++i != 11);
>
> Or, if you'd prefer the less efficient simplicity of:
>
> for(unsigned i = 0; i != 11; ++i)
> {
>
> }
>
> You're still pretending that you're array has eleven elements though...
>
> --
>
> Frederick Gotham


 
Reply With Quote
 
c
Guest
Posts: n/a
 
      09-24-2006
Thank you for helping me, I'm going to try and reply again if I have
some Questions.

Ancient_Hacker wrote:
> oops, I forgot to log the address of each created string, try this:
>
>
> char BigPool[ 1000000 ]; // or whatever size you expect;
>
> char Strs[ STRINGS_IN_LIST ] = { "foo", "bar", ........ };
>
> char * OutStrs[ STRINGS_IN_LIST ];
>
>
> int Free = 0;
>
> int i, Len;
>
> for( i = 0; i < STRINGS_IN_LIST; i++ )
> Free += sprintf( OutStrs[ i ] = BigPool[ Free ], "%s%s", argv[
> whatever ], Strs[ i ] ) + 1;
>
>
>
> .


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-24-2006
c wrote:
> Hi everybody.
>
> I'm working on converting a program wriiten on perl to C, and facing a
> problem with concatenate strings.
> Now here is a small program that descripe the problem, if you help me
> to solve in this small code, I can solve it on my own program...you
> don't want to have head-ache
>
> So, the problem excatly is, I built an array..just like this one.
>
> char *my_array [10];
> my_array[0] = "Sally\n";
> my_array[1] = "Ndiya\n";
> my_array[2] = "Samantha\n";
> my_array[3] = "Sara\n";
> my_array[4] = "Cadillac\n";
> my_array[5] = "GM For Ever\n";
> my_array[6] = "SlacWare\n";
> my_array[7] = "Google\n";
> my_array[8] = "Google more and more\n";
> my_array[9] = "You\n";
> my_array[10] = "Computers\n";


"Just like" this one? Including the attempt to store a
value in the non-existent eleventh element of a ten-element
array? In C an array of N elements has indices that run from
[0] through [N-1], inclusive, but does not have an [N] element.

By the way, C offers a more convenient syntax for array
initialization:

char *my_array[10] = { "Sally\n", "Ndiya\n", "Samantha\n",
"Sara\n", "Cadillac\n", "GM For Ever\n", "SlacWare\n",
"Google\n", "Google more and more\n", "You\n",
"Computers\n", };

A few points about this: First, since you've declared the array
to have ten elements but actually provided eleven initializers,
the compiler will complain and point out your mistake. Second,
you could replace [10] with [] and let the compiler figure out
the array size by counting the initializers itself instead of
making you do it (sizeof my_array / sizeof my_array[0] will then
tell you how big the compiler decided the array should be). And
last, the comma after the final initializer is optional -- it's
sometimes convenient if the code is being generated by a program
or if you anticipate adding more initializers later, but you can
use it or omit it according to your own taste.

> my program will recieve an argument -char *argv[]- from the user.
>
> int main(int argc,char *argv[])
>
> I want to concatenate the given argument to at the first of each line
> of my array. [...]


You need to confront the simplicity of C's memory management.
I mean "simplicity" in the sense that C doesn't do very much about
memory management on its own; you must attend to it for yourself.
This is both a weakness and a strength of C: It's a weakness because
it forces you to fret about the details, and a strength because it
doesn't burden you with the machinery of automatic managers whose
style may not suit your needs.

In this case, C has already done some minimal memory management
for you: It has set aside space for the array my_array and filled
its elements with pointers to strings, and it has set aside space
for those strings and filled them with their intended contents. But
now you need something different: You need to create new strings in
memory someplace, and change the array elements to point to those
new strings instead of the old ones.

Here's a quickie solution, with explanation to follow:

for (i = 0; i < sizeof my_array / sizeof my_array[0]; ++i) {
char *new;
new = malloc(strlen(argv[1]) + strlen(my_array[i]) + 1);
if (new == NULL)
exit (EXIT_FAILURE);
strcpy (new, argv[1]);
strcat (new, my_array[i]);
my_array[i] = new;
}

The first line steps the variable i through all the valid index
values of the array my_array, executing the body of the loop once
for each i value. The second line declares a temporary variable called
new that will point to the memory for one of the new strings you're
about to create -- you can't just obliterate my_array[i] yet, because
you still need to work with the original string it's still pointing to.

The third line requests some memory to be allocated. How much?
You'll need enough for all the characters of the argv[1] string, plus
enough for all the characters of the my_array[i] string, plus one more
for the '\0' that must be present at the end of any string. (It is
astonishing how frequently people forget about that "plus one;" be
on guard against making that all-too-commmon mistake.)

The fourth and fifth lines are important: You call malloc to
*request* a certain amount of memory, but the request might not be
granted. Perhaps the system is running low on memory, perhaps it
has plenty available but it's all scattered around in dribs and
drabs none of which are big enough, perhaps the phase of the Moon
is wrong. *Always* check for a NULL return from malloc, and take
corrective action if you get one -- in this case, the "corrective
action" is simply to terminate the program unsuccessfully, but in
other programs you might do something more elaborate.

The sixth and seventh lines build the new string from the two
pieces. Line six copies the argv[1] string into the new memory area,
and line seven appends the my_array[i] string to it. Note carefully
that line six uses strcpy and seven uses strcat; it would be wrong
to interchange them or to use the same function (either one) in both
places. You need to copy the first string into the uninitialized
brand-new memory area, and then append the second.

Finally, line eight changes my_array[i] so it now points to the
newly-built string.

At the beginning of the program, you'll need to #include the
<string.h> header (to declare the strlen, strcpy, and strcat functions)
and the <stdlib.h> header (for malloc, exit, and EXIT_FAILURE). You
may need other headers like <stdio.h> if you intend to display any
results. Also, it would be a good idea for your program to check
that it actually received an argument string (check for argc>1)
before running blindly ahead and trying to do things with a string
that doesn't even exist.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-24-2006
c wrote:
>
> I'm working on converting a program wriiten on perl to C, and
> facing a problem with concatenate strings.


Try strlcat and strlcpy. You can get source and documentation for
these from:

<http://cbfalconer.home.att.net/download/>

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>


 
Reply With Quote
 
c
Guest
Posts: n/a
 
      09-24-2006
THANK YOU VERY VERY MUCH, I'm going to try it and reply if I have
another questions..you just wrote an article for me, it must be printed


Eric Sosman wrote:
> c wrote:
> > Hi everybody.
> >
> > I'm working on converting a program wriiten on perl to C, and facing a
> > problem with concatenate strings.
> > Now here is a small program that descripe the problem, if you help me
> > to solve in this small code, I can solve it on my own program...you
> > don't want to have head-ache
> >
> > So, the problem excatly is, I built an array..just like this one.
> >
> > char *my_array [10];
> > my_array[0] = "Sally\n";
> > my_array[1] = "Ndiya\n";
> > my_array[2] = "Samantha\n";
> > my_array[3] = "Sara\n";
> > my_array[4] = "Cadillac\n";
> > my_array[5] = "GM For Ever\n";
> > my_array[6] = "SlacWare\n";
> > my_array[7] = "Google\n";
> > my_array[8] = "Google more and more\n";
> > my_array[9] = "You\n";
> > my_array[10] = "Computers\n";

>
> "Just like" this one? Including the attempt to store a
> value in the non-existent eleventh element of a ten-element
> array? In C an array of N elements has indices that run from
> [0] through [N-1], inclusive, but does not have an [N] element.
>
> By the way, C offers a more convenient syntax for array
> initialization:
>
> char *my_array[10] = { "Sally\n", "Ndiya\n", "Samantha\n",
> "Sara\n", "Cadillac\n", "GM For Ever\n", "SlacWare\n",
> "Google\n", "Google more and more\n", "You\n",
> "Computers\n", };
>
> A few points about this: First, since you've declared the array
> to have ten elements but actually provided eleven initializers,
> the compiler will complain and point out your mistake. Second,
> you could replace [10] with [] and let the compiler figure out
> the array size by counting the initializers itself instead of
> making you do it (sizeof my_array / sizeof my_array[0] will then
> tell you how big the compiler decided the array should be). And
> last, the comma after the final initializer is optional -- it's
> sometimes convenient if the code is being generated by a program
> or if you anticipate adding more initializers later, but you can
> use it or omit it according to your own taste.
>
> > my program will recieve an argument -char *argv[]- from the user.
> >
> > int main(int argc,char *argv[])
> >
> > I want to concatenate the given argument to at the first of each line
> > of my array. [...]

>
> You need to confront the simplicity of C's memory management.
> I mean "simplicity" in the sense that C doesn't do very much about
> memory management on its own; you must attend to it for yourself.
> This is both a weakness and a strength of C: It's a weakness because
> it forces you to fret about the details, and a strength because it
> doesn't burden you with the machinery of automatic managers whose
> style may not suit your needs.
>
> In this case, C has already done some minimal memory management
> for you: It has set aside space for the array my_array and filled
> its elements with pointers to strings, and it has set aside space
> for those strings and filled them with their intended contents. But
> now you need something different: You need to create new strings in
> memory someplace, and change the array elements to point to those
> new strings instead of the old ones.
>
> Here's a quickie solution, with explanation to follow:
>
> for (i = 0; i < sizeof my_array / sizeof my_array[0]; ++i) {
> char *new;
> new = malloc(strlen(argv[1]) + strlen(my_array[i]) + 1);
> if (new == NULL)
> exit (EXIT_FAILURE);
> strcpy (new, argv[1]);
> strcat (new, my_array[i]);
> my_array[i] = new;
> }
>
> The first line steps the variable i through all the valid index
> values of the array my_array, executing the body of the loop once
> for each i value. The second line declares a temporary variable called
> new that will point to the memory for one of the new strings you're
> about to create -- you can't just obliterate my_array[i] yet, because
> you still need to work with the original string it's still pointing to.
>
> The third line requests some memory to be allocated. How much?
> You'll need enough for all the characters of the argv[1] string, plus
> enough for all the characters of the my_array[i] string, plus one more
> for the '\0' that must be present at the end of any string. (It is
> astonishing how frequently people forget about that "plus one;" be
> on guard against making that all-too-commmon mistake.)
>
> The fourth and fifth lines are important: You call malloc to
> *request* a certain amount of memory, but the request might not be
> granted. Perhaps the system is running low on memory, perhaps it
> has plenty available but it's all scattered around in dribs and
> drabs none of which are big enough, perhaps the phase of the Moon
> is wrong. *Always* check for a NULL return from malloc, and take
> corrective action if you get one -- in this case, the "corrective
> action" is simply to terminate the program unsuccessfully, but in
> other programs you might do something more elaborate.
>
> The sixth and seventh lines build the new string from the two
> pieces. Line six copies the argv[1] string into the new memory area,
> and line seven appends the my_array[i] string to it. Note carefully
> that line six uses strcpy and seven uses strcat; it would be wrong
> to interchange them or to use the same function (either one) in both
> places. You need to copy the first string into the uninitialized
> brand-new memory area, and then append the second.
>
> Finally, line eight changes my_array[i] so it now points to the
> newly-built string.
>
> At the beginning of the program, you'll need to #include the
> <string.h> header (to declare the strlen, strcpy, and strcat functions)
> and the <stdlib.h> header (for malloc, exit, and EXIT_FAILURE). You
> may need other headers like <stdio.h> if you intend to display any
> results. Also, it would be a good idea for your program to check
> that it actually received an argument string (check for argc>1)
> before running blindly ahead and trying to do things with a string
> that doesn't even exist.
>
> --
> Eric Sosman
> (E-Mail Removed)lid


 
Reply With Quote
 
Joe Estock
Guest
Posts: n/a
 
      09-24-2006
Frederick Gotham wrote:
> c posted:
>
>> char *my_array [10];

>
>
> This array contains ten elements.
>
>
>> my_array[0] = "Sally\n";
>> my_array[1] = "Ndiya\n";
>> my_array[2] = "Samantha\n";
>> my_array[3] = "Sara\n";
>> my_array[4] = "Cadillac\n";
>> my_array[5] = "GM For Ever\n";
>> my_array[6] = "SlacWare\n";
>> my_array[7] = "Google\n";
>> my_array[8] = "Google more and more\n";
>> my_array[9] = "You\n";
>> my_array[10] = "Computers\n";

>
>
> Here, you assign to eleven separate elements.


To someone who is new at C, the above code would not be as easy to
notice something wrong in.

In c array indexing starts at 0. When you declare char *my_array[10];
you are saying the following: "Give me an array of 10 pointers to type
char". You can now safely assign to 0 - 9 of those without worrying.
Assigning to anything beyond index 9 would invoke UB (Undefined Behaviour).

It is also important for you to note that in your declaration you are
(in laymen terms) saying: "Give me an array of 10 pointers to type char
that I may not modify after initialization".

For example:

char *foo;

/* this is fine. you are initializing foo */
foo = "some string";

/* this is fine too. you are declaring and initializing foo */
char *foo = "some string";

/* this is *not* fine. you are writing to memory that you shouldn't */
foo[3] = '\0';

char foo[100];

/* the below is fine. we are allowed to write to foo provided that we do
not go beyond element 99. */
strcpy(foo, "testing");

foo[4] = '\0';

>
>
>> #include <stdio.h>
>>
>> int main(int argc,char *argv[])
>> {int i=-1;
>> char *my_array [10];
>>
>> my_array[0] = "Sally\n";
>> my_array[1] = "Ndiya\n";
>> my_array[2] = "Samantha\n";
>> my_array[3] = "Sara\n";
>> my_array[4] = "Cadillac\n";
>> my_array[5] = "GM For Ever\n";
>> my_array[6] = "SlacWare\n";
>> my_array[7] = "Google\n";
>> my_array[8] = "Google more and more\n";
>> my_array[9] = "You\n";
>> my_array[10] = "Computers\n";

>
>
> Again here, you assign to eleven elements.
>
>
>> while(i++ < 10)
>> {
>> printf(argv[1]); //Don't do it like this..read my comments please.
>> printf(my_array[i]);
>> }

>
>
> Why resort to that? Looping 0 through 10 can be much simpler:
>
> unsigned i = 0;
>
> do {
>
> }while(++i != 11);


This isn't safe, especially if you're suggesting that someone who is
obviously new to C to use it. What happens if OP increments i within the
while loop? Consider the following:

i = 0;

do
{
i += 20;
} while(++i != 11);

There you have an infinite loop and the OP may not know what is wrong.
The code below would give clearer meaning of your intent.

i = 0;

do
{
/* code */
} while (++i < 11);

>
> Or, if you'd prefer the less efficient simplicity of:
>
> for(unsigned i = 0; i != 11; ++i)


Not valid C89 or C90 syntax. Variable declarations must be at the
beginning of a block. Again you insist on checking i to see if it is
*exactly* 11.

> {
>
> }
>
> You're still pretending that you're array has eleven elements though...
>


It would be more helpful if you could point out to the OP *why* (see my
reply towards the top).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenating strings EHC Python 3 12-16-2006 12:07 AM
Concatenating strings John Henry Python 1 07-01-2006 03:38 AM
Concatenating strings John Henry Python 1 07-01-2006 03:36 AM
adding strings, not concatenating them Stan Horwitz Perl 2 02-15-2006 08:38 PM
Problems concatenating strings stroker_ace@hotmail.com C++ 11 04-08-2005 03:32 PM



Advertisments