EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

String Concatenation in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » String Concatenation in C

String-Concatenation-in-C

Introduction on String Concatenation in C

In the C Programming Language, the string concatenation is the process of joining/concatenating character strings from one end to another end. The strcat function joins the copy of string pointed by string_2 to the end of the string pointed by string_1 and it returns a pointer to string_1. For concatenation of string, we are using strcat function with the use of header function string.h. The process of concatenation is also referred to as binary addition of string with the use of + sign, for example, String + Concatenation = String Concatenation. Let’s see the syntax of the strcat function below,

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

char *strcat(char *string1, const char *string2);

The required header file for the strcat function is,

#include <string.h>

Here the parameters of the syntax explain that the string1 a pointer to string which will be altered and string2 will be copied to the end of string1, string2 a pointer to a string which will be added to the end of string1. Finally, the strcat function returns a result of a pointer to string1.

How Does String Concatenation Work in C?

In C-Programming the concatenation of string works with given strings as a single result with the use of strcat() function. The first and foremost thing is to include the header files required for the program which is pre-processor directive stdio.h and string.h, the header file string.h describes that variable type, macro, and several functions to operate arrays of characters in the program.

Code:

#include <stdio.h>
#include<string.h>
int main()
{
char str1[100], str2[100];
printf("First String: \n"); gets(str1);
printf("Second String: \n"); gets(str2);
strcat(str1,str2);
printf("Concatenation of both string is %s\n", str1);
return 0;
}

Code Explanation: The important function is the main() which is declared as integer to return the result as integer value at the termination of the program. In the main() function, we declare the character arrays as str1[] and str2[] which have the memory location. For the displaying purpose you have to use the printf() statements, the statement gets(str1) requires for fetching the characters as string and store it in arraystr1[]. Likewise the another gets(str2) for fetching another string and store it in array str2[]. Finally, with the use of strcat() function we concatenating the strings by following the syntax char * strcat(str1,str2) here the str1 defines the destination array it contains the string_1 and results the concatenated string, then str2 also contains string for concatenating, at the end of program it returns 0 as an integer type value. Finally, the output will be as shown below,

Output:

String Concatenation in C working

String Concatenation in C Using Various Methods

In C-Programming the finest approach to concatenate two strings is by using strcat() function. Let’s see the example to concatenate two strings manually without using strcat() function.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Example #1

Here, the strings string_1 and string_2 get concatenated and the final result is stored in string_1. The main thing that the length of string_1 should enough to store the string after the process of concatenation, otherwise you will get an unexpected result.

Code:

#include <stdio.h>
int main()
{
char string_1[100]="Code", string_2[]="Analysis";
int i,j;
for(i=0;string_1[i]!='\0';++i)
//to store length of string_1 in i
{
printf("i=%d\n",i);
}
// to concatenating characters of string_2 to string_1
for(j=0;string_2[j]!='\0';++j,++i)
{
string_1[i]=string_2[j];
}
// to finish string_1 string
string_1[i] = '\0';
printf("Result: Concatenation of Strings: ");
puts(string_1);
return 0;
}

Output:

String Concatenation in C eg1

Example #2

This program is used to concatenate two given strings by using the strlen() function. Have to include the string.h header file; it categorizes various functions to work with arrays of characters with the program. Then to define the main() function and declared int as return type at the end of the program. The inside the main() function, you have to take two character arrays name string_1[] and string_2[] to allocate a memory location.

Code:

#include <stdio.h>
#include <string.h>
int main()
{
char string_1[50] = "Programming";
char string_2[] = "Language";
int i, j, a,b;
a = strlen(string_1);
b = strlen(string_2);
j = 0;
for(i = a; i< a+b; i++ )
{
string_1[i] = string_2[j];
j++;
}
string_1[i] = '\0';
printf("%s", string_1);
return 0;
}

Output:

String Concatenation in C eg2

Example #3

This program is to concatenate two given strings using pointers. Previously the program describes the approach to concatenate two strings by various methods. In this below program, it takes two strings to concatenate and it stores with pointers actualString and toAppend. The function stringConcatenation() takes 2 parameters that one keeps as the reference and another one traces till the end. Finally, it appends both strings as a result.

Code:

#include <stdio.h>
void stringConcatenation(char*, char*);
int main()
{
char actualString[100], toAppend[100];
printf("Source String : \n");
gets(actualString);
printf("String to Concatenate : \n");
gets(toAppend);
stringConcatenation(actualString, toAppend);
printf("Result:\n"" String Concatenation: \"%s\"\n", actualString);
return 0;
}
void stringConcatenation(char *actualString, char *toAppend)
{
while(*actualString)
actualString++;
while(*toAppend)
{
*actualString = *toAppend;
toAppend++;
actualString++;
}
*actualString = '\0';
}

Output:

Output

Example #4

The concatenation of two strings without using of strcat() function, the process of concatenation with the given two strings string_1, string_2 and the third-string string_3 is for storing the resultant concatenation strings. Finally, it displays the concatenated string.

Given Strings:

String_1="Welcome"
String_2="Strings"

Output: WelcomeStrings

Code:

#include <stdio.h>
int main()
{
// to get the two Strings to be concatenated
char string_1[100] = "String", string_2[100] = "Concatenation";
// to declare a new String for the storage purpose of  the concatenated String
char string_3[100];
int i = 0, j = 0;
printf("\nFirst string: %s", string_1);
printf("\nSecond string: %s", string_2);
// to insert the first string in the new string
while (string_1 [i] != '\0') {
string_3[j] = string_1[i];
i++;
j++;
}
// to insert the second string in the new string
i = 0;
while (string_2[i] != '\0') {
string_3[j] = string_2[i];
i++;
j++;
}
string_3[j] = '\0';
// to print the concatenated string
printf("\nConcatenated string: %s", string_3);
return 0;
}

Output:

Output

Conclusion

In C Programming, we learned about the String Concatenation method with and without the usage of strcat() function with the examples and how to make use of it. I hope the Concatenation of string with various methods would help out you with those examples.

Recommended Articles

This is a guide to String Concatenation in C. Here we discuss the syntax, working, and methods of String Concatenation in C along with the examples and code implementation. You may also look at the following articles to learn more –

  1. Examples of C Union
  2. Left Shift Operator in C
  3. Decimal to Octal in C
  4. Java String Concatenation

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More