[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

problem with concatenating two strings (character arrays) using strcat

Posted on 2004-10-24
15
Medium Priority
?
402 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:somanchivasu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
15 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 336 total points
ID: 12394698
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
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 332 total points
ID: 12395509
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
 

Author Comment

by:somanchivasu
ID: 12396182
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:somanchivasu
ID: 12396259
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12396269
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12396303
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
 

Author Comment

by:somanchivasu
ID: 12396319
so i think i shall put 2 different values index, index1??? how to place digits in same order into c[index]?

thanks
0
 

Author Comment

by:somanchivasu
ID: 12396333
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
 

Author Comment

by:somanchivasu
ID: 12396368
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12396462
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12398364
Change your loop to:
for(index = 0;index> 3; index++)

With this you should get the correct direction!

-ssnkumar
0
 

Author Comment

by:somanchivasu
ID: 12418654
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
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 332 total points
ID: 12419505
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question