Solved

problem with concatenating two strings (character arrays) using strcat

Posted on 2004-10-24
384 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
Question by:somanchivasu
    13 Comments
     
    LVL 45

    Accepted Solution

    by:
    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
    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
    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
     

    Author Comment

    by:somanchivasu
    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 45

    Expert Comment

    by:Kdo
    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 45

    Expert Comment

    by:Kdo
    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
    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
    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
    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 45

    Expert Comment

    by:Kdo
    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
    Change your loop to:
    for(index = 0;index> 3; index++)

    With this you should get the correct direction!

    -ssnkumar
    0
     

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    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…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now