-1

I was trying to create a program that takes in two arrays and concatenates them to create a new string. These are the two strings.

 char a[8]={"hellostr"};
 char b[8]={"HELLOSTR"};

Can someone tell me how could I concatenate and display this concatenated string? I tried looking for it but could not understand much of it.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Anamika Ana
  • 165
  • 1
  • 3
  • 11

2 Answers2

3

You need to make use of strcat() function from string.h.

A sample algo:

  1. Define an array (say destarr[128], for example)large enough to hold the result (concatenated string).
  2. memset()the destarr to 0.
  3. use strcat(destarr, a) and strcat(destarr, b) to concatenate one after another.

That said,

 char a[ ]={"hellostr"};

is considered better and less error-prone over

char a[8]={"hellostr"};

as,

  • In the former case, compiler takes the charge to allocate the memory, as required, with the null-terminator in mind. The array can be used as a string.
  • In the later case, there is no room for the null-terminator and hence, the array can neither be considered nor be used as a string.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thank you for that example. I had one more question. Now if I want to display just one of the characters from this concatenated string how do I do that? – Anamika Ana Jul 08 '15 at 14:25
  • I don't agree that the former is less error prone. Because if you intended to have a string exactly x characters long, you don't want code with more characters than that to compile. For example the bugged code `char a[8]={"hellostr"};` should yield a compiler diagnostic, so that hopefully the programmer gets a slap: "hey, you didn't consider null terminarion! Go fix your code to a[8+1]." rather than "everything looks ok here, proceed even though there's potentially a bug here". – Lundin Jul 08 '15 at 14:27
  • @AnamikaAna `one of the characters` is that what you meant exactly? use index, naturally. – Sourav Ghosh Jul 08 '15 at 14:27
  • @SouravGhosh Actually gcc throws no warning for this with max warnings enabled. Very strange. – Lundin Jul 08 '15 at 14:32
  • @Lundin it should show something along the line of _"excess initializer for array....."_, right? – Sourav Ghosh Jul 08 '15 at 14:35
  • @SouravGhosh Indeed. Peculiar since it does so if you write the string as `{'h','e','l','l','o','s','t','r','\0'}`. – Lundin Jul 08 '15 at 15:02
  • 1
    @SouravGhosh I've posted a question [here](http://stackoverflow.com/questions/31296727/inconsistent-gcc-diagonistic-for-string-initialization), maybe someone out there knows more :) – Lundin Jul 08 '15 at 15:11
0

strcat function can be used.

Header -string.h

If you want to do that without strcat function then you can write a simple function -

  void concatenate(char a[], char b[])
  {
     int c, d;

      c = 0;

     while (a[c] != '\0')
   {
        c++;    
   }

     d = 0;

   while (b[d] != '\0') 
    {
       a[c] = b[d];
       d++;
       c++; 
    }

       a[c] = '\0';
  }
ameyCU
  • 16,489
  • 2
  • 26
  • 41