Concatenating Strings in C

Concatenation means to join something / two things usually. If str1 and str2 are two strings the result of concatenation operation is a string which contains characters belonging to the first as well as second string; lined one after the other. The resultant is a new string.

It is also called as Binary Addition of Strings.

First let us see the simpler approach of using pre-defined C functions.

 

Approach 1: Using the functions strcat() and strncat() from string.h.

  • strcat() and strncat both are built in functions in order to concatenate strings.
  • This is again a not recommended approach as they are from Microsoft C library and does not work in standard C library. If you are using a Turbo C++ IDE, only then will the below approach work. The GNU library has no such function defined and will throw you an error.
  • Here to scan the string we are using fgets method in order to understand how it is used, although it is not recommended however it is better than gets().

Syntax:

strcat( destination_string, source_string);

Using the above syntax, the destination string will be followed by the source string i.e a copy of source string is appended to the end of destination string.

Return value is destination string.

strncat( destination_string, source_string, size_t maxlen);

Using the above syntax, the destination string will be followed by the source string’s maxlen characters i.e a copy of source string’s maxlen characters

is appended to the end of destination string. Return value is destination string.

 

We shall see examples of the above in the below code.

Code:

#include <stdio.h>

#include<conio.h>

#include <string.h>                    //include the header file which contains definition of strcat and stncat  function

int main()

{

    clrscr();

    char string1[100], string2[100] ;

            printf("Str1 : \n");

            fgets(string1, 100, stdin);     //read string input from standard input screen.

            printf("Str2 : \n");

            fgets(string2, 100, stdin);     //read string input from standard input screen.

            strcat(string1, string2);      //line 16

            printf("Concatenated string using string 1 destination and string 2 Source :\n");

            puts(string1);  //modification done in destination string.

    getch();

    return  0;

}

Output: 

Using same input as above we can modify //line 16 code by the below examples and see varied outputs.

 

Using string 2 as destination and string 1 as source:

strcat(string2, string1);

puts(string2);

 

Using string 1 as destination and string 2 as source using only first 3 characters of string 2:

strncat(string1, string2, 3);

puts(string1);

 

 

Using string 2 as destination and string 1 as source using only first 4 characters of string 1:

strncat(string2, string1, 4);

puts(string2);

 

 

Approach 2:  Without using pre-defined functions of C.

  • In this approach we will first accept two strings str1 and str2.
  • We are considering str1 to be the destination string and str2 to be the source string.
  • We have to hence declare these strings of pre-defined sizes with space which can accommodate a large string as well.
  • bytes_read is an integer which stores the value of number of blocks utilized by the first string. Since it includes newline character, hence length of string 1 is bytes_read – 1.
  • Alternatively, you may iterate a counter as you traverse through first string in order to calculate length of string 1.
  • Since we have the length of first string calculated we can now proceed for concat operation.
  • We traverse each element of our second string and start storing the characters at position starting from position string1[length], because at that position we have newline character of first string; marking the end of first string.
  • Last but not the least, we terminate str1 by appending null ‘\0’ character manually.

Code:

#include <stdio.h>

int main()

{

char *str1, *str2;

int i, j, size = 100, bytes_read;



printf("Enter first string: ");

str1 = (char *) malloc (size);

bytes_read = getline (&str1, &size, stdin);



i = bytes_read - 1;    //since blocks read by getline will also include null character; hence

length of string is bytes_read - 1.



printf("Enter second string: ");

str2 = (char *) malloc (size);

getline (&str2, &size, stdin);



for(j = 0; str2[j] != '\0'; j++, i++)

{

str1[i] = str2[j];

}



str1[i] = '\0';             //to terminate resultant concatenated string



printf("After concatenation string is: %s", str1);



return 0;

}

Output:

Enter first string: Hello!

Enter second string: How are you ?

After concatenation string is: Hello!How are you ?

Approach 3: Using Pointers:

  • The logic is same as used above; however we do the same with pointers .
  • Here , you notice that we just increment the pointers to the strings. src is the pointer to source string.
  • Notice that after we have reached end of first string which is checked by condition while(*src) ; wherein if null character is encountered the counter for string length stops.
  • Immediately whe have decremented src by 1 because ; using getline() function stored newline character in the last block read. Hence we decrement it. In order to start storing the characters of str2, replacing newline character by string 2’s first character.

Code:

#include <stdio.h>

void concatenate(char *src, char *dest)

{

while(*src)

src++;



src--;   //decrementing as the last block read contains new line character of first string.



while(*dest)

{

*src = *dest;

src++;

dest++;

}



*src = '\0';



}

int main()

{

char *str1, *str2;

int size = 100, bytes_read;



printf("Enter first string: ");

str1 = (char *) malloc (size);

bytes_read = getline (&str1, &size, stdin);



printf("Enter second string: ");

str2 = (char *) malloc (size);

getline (&str2, &size, stdin);



concatenate(str1, str2);



printf("After concatenation string is: %s", str1);



return 0;

}

Output:

Enter first string: Hello!

Enter second string: Bye!

After concatenation string is: Hello!Bye!

 

We have seen above various methods to perform string concatenation operation in C.

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019