Concatenate Of Two String


Explanation

String concatenation means joining two or more strings together to create a single string. In C language, the strcat() function is used to combine two strings, and the strcpy() function is used to copy one string into another.

In this program, the user enters the first name and last name separately. The program combines both strings using strcat() and stores the final full name using strcpy().

How It Works?

  • Step 1: Declare three character arrays Lname, Fname, and full to store strings.
  • Step 2: Accept the last name from the user.
  • Step 3: Accept the first name from the user.
  • Step 4: Add a space between first name and last name using strcat().
  • Step 5: Join the last name with the first name using strcat().
  • Step 6: Copy the final string into the full variable using strcpy().
  • Step 7: Display the complete name.

Example Program


#include <stdio.h>
#include <conio.h>
#include <string.h>

main()
{

    char Lname[20], Fname[20], full[20];

    clrscr();

    printf("Enter the Last Name : ");

    scanf("%s", Lname);

    printf("\nEnter the First Name : ");

    scanf("%s", Fname);

    strcat(Fname, " ");

    strcat(Fname, Lname);

    strcpy(full, Fname);

    printf("\nFull Name Is %s", full);

    getch();

}

Output

Enter the Last Name : Shaikh

Enter the First Name : Hamid

Full Name Is Hamid Shaikh

Practice Exercises

Task 1: Write a C program to accept two strings from the user and concatenate them into a single string using the strcat() function. Display the final combined string.

Goal: Learn how to combine two strings using string handling functions in C language.

💡 Hint: Declare three character arrays. Accept two strings from the user. Use strcat() to join the second string with the first string and display the final concatenated string.
💡 Show Solution

#include <stdio.h>
#include <conio.h>
#include <string.h>

main()
{

    char first[20], second[20];

    clrscr();

    printf("Enter First String : ");

    scanf("%s", first);

    printf("\nEnter Second String : ");

    scanf("%s", second);

    strcat(first, second);

    printf("\nCombined String Is %s", first);

    getch();

}

Output
Enter First String : Hello

Enter Second String : World

Combined String Is HelloWorld
Explanation: The program accepts two strings from the user and combines them using the strcat() function. The second string is added at the end of the first string, and the final combined string is displayed.