problem with concatenating two strings (character arrays) using strcat

Hi all,

I am trying to concatenate two character arrays .. they are getting concatenated when i used strcat but when i tried to print them they were being printed in reverse order.. my code for this is as follows:

 significant_digits = decimal2binary(packet.version,binary_value);
  for(index=3;index>=0;index--)
  {
   c[index] = binary_value[index];
   //printf("%c", c[index]);
  }
  //printf("%s", c);
  c[4] = '\0';
  packet.headerlength = 5;
  significant_digits = decimal2binary(packet.headerlength,binary_value);
  for(index=3;index>=0;index--)
  {
   a[index] = binary_value[index];
  //printf("%c", a[index]);
  }
  a[4] = '\0';
  strcat(c,a);
  puts(c);

the function decimal2binary() is converting decimal to binary number which i am storing in a character array. hope you can understand what i am saying and will be very happy if u can help me out in this.

Thanks
somanchivasuAsked:
Who is Participating?
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
Hi somanchivasu,

The definition of the strcat() function is

char *strcat (char *dest, const char *source)

The second parameter is a "const char".  It doesn't change.

The function appends the contents of the second parameter to the contents of the first parameter.  (As a general rule, when parameters to C functions pass addresses that can change, the first parameter is the one that will change.  There are several exceptions.)

Swap things around so that you do a an strcap (a, c), then puts(a).



Good Luck!
Kent
0
 
MysidiaConnect With a Mentor Commented:
Um:
  for(index=3;index>=0;index--)
 {
   c[index] = binary_value[index];

it looks like you're trying to build them in backwards order, since
you're starting at 3 and ending at 0

but of course this won't reverse the order of  binary_value[]
since you're mapping one index in one to the same index in the other

as a general matter, for example:

char S[10];
strcpy(S, "abc");
strcat(S, "def");

yields    S equal: abcdef

not any of these..
      defabc
      abcfed
      cbadef
      cbafed



0
 
somanchivasuAuthor Commented:
hi I hav put strcat(c,a) in reverse order and tried too but i dint get it....

and in the code

 for(index=3;index>=0;index--)
 {
   c[index] = binary_value[index];
 
i am setting index as 3 (fixed) to make c[index] fixed to 3...but not reversing....do u think its reversing the order and placing in c[index]....if so is there any other solution??

thanks
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
somanchivasuAuthor Commented:
in the code

for(index=3;index>=0;index--)
 {
   c[index] = binary_value[index];
    printf("%c", c[index]);
  }
 
when i am giving printf inside the for loop its giving me the same order but when i am print it as a string outside the loop like

puts(c);

then its giving in reverse order....help me out plz

thanks
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi somanchivasu,

The arrays (strings) aren't being printed in reverse order.  The data in the arrays (strings) is in reverse order from what you are expecting.

Note the original loop:

  for(index=3;index>=0;index--)
  {
   c[index] = binary_value[index];
   //printf("%c", c[index]);
  }

You're placing the first digin in c[3], the next in c[2], the next in c[1], and the last in c[0].  If the starting value is 1357, you'll have placed it in the array as 7531.  This is where your "reversing" is occurring.


Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi somanchivasu,

It's very easy to demonstrate this:

  strcpy (c, "abcd");
  for(index=3;index>=0;index--)
  {
   c[index] = binary_value[index];
   printf(" Array contains: %s\n", c);
  }

One character at a time, you'll see your array change from "abcd" to the reverse of what you are expecting.

If you'll change the for() statement to move forward through the array instead of backward, this problem should "go away".


Kent
0
 
somanchivasuAuthor Commented:
so i think i shall put 2 different values index, index1??? how to place digits in same order into c[index]?

thanks
0
 
somanchivasuAuthor Commented:
i have changed for statement to move forward .... like

for(index=0;index<=3;index++)

but its giving the same output.... i think its because i hav given index to both binary_value and also c...is it so??
0
 
somanchivasuAuthor Commented:
hi kent

in the code

for(index=3;index>=0;index--)
 {
   c[index] = binary_value[index];
    printf("%c", c[index]);
  }
 
when i am giving printf inside the for loop its giving me the same order but when i am print it as a string outside the loop like

puts(c);

or

printf("%s" ,c);

then its giving in reverse order... what do u think where the error might be?

thanks
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi somanchivasu,

The problem is in perception, not coding.  The printf() statement inside the for loop is printing the characters in the order that you are processing them in the loop.  This is not necessarily the order that the characters are positioned in the string.

Look at the following code:


int  idx;
char *SomeString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (idx = 0; idx < 26; idx++)
  printf ("%c", SomeString[idx]);

for (idx = 26; idx >= 0; idx--)
  printf ("%c", SomeString[idx]);

The first for() loop prints the alphabet one character at a time from someString.  The second for() loop prints the alphabet backwards, one character at a time from the stame string.  Yet the string isn't changed.

This appears to be what you're doing -- printing the characters of the string from within the for() loop in the order that you encounter the characters.


Kent
0
 
ssnkumarCommented:
Change your loop to:
for(index = 0;index> 3; index++)

With this you should get the correct direction!

-ssnkumar
0
 
somanchivasuAuthor Commented:
hi all,

I want to store some large number of characters into a character buffer array... i want to use pointers ..

I am converting some decimal numbers into binary values but now i want to store these binary values into an array which should be of infinite length... i have typecasted the binary values in char...  basically i can store these binary values into arrays of 16  bit length as i need to  sum every 16 bits of the binary values to each of the other 16 bits of binary values i generated.... for example... i have generated binary values of 16 4 13 56.... now i need to store the binary values of these numbers into an array and then split them 16 16 bits and add them...

I hope u got my point....would be very happy if u can help me out..

Thanq
0
 
ssnkumarConnect With a Mentor Commented:
Looks like there are two problems:
1. I want to store some large number of characters into a character buffer array... i want to use pointers ..
2. I am converting some decimal numbers into binary values but now i want to store these binary values into an array which should be of infinite length

And "infinite length" is not possible, because your computer memory is limited!

If you can take up one or two examples and explain, then we can think of a way of solving this.

-ssnkumar
0
All Courses

From novice to tech pro — start learning today.