C Program to Concatenate Two Strings

Introduction

Concatenating two strings means joining them end-to-end to form a single string. This guide will show you how to write a C program to concatenate two strings provided by the user.

Problem Statement

Create a C program that:

  • Takes two strings as input from the user.
  • Concatenates the second string to the end of the first string.
  • Displays the concatenated string.

Example:

  • Input:
    • String1 = "Hello, "
    • String2 = "World!"
  • Output: Concatenated String = "Hello, World!"

Solution Steps

  1. Include the Standard Input-Output and String Libraries: Use #include <stdio.h> for standard input-output functions and #include <string.h> for string functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the two input strings and the concatenated string.
  4. Input the Strings: Use gets or scanf to take input from the user for both strings.
  5. Concatenate the Strings:
  • Method 1: Use the strcat function from the string.h library.
  • Method 2: Manually concatenate the strings by appending characters of the second string to the first string.
  1. Display the Concatenated String: Use printf to display the concatenated string.

C Program Using strcat Function

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

int main() {
    // Step 1: Declare variables to hold the two input strings
    char str1[100], str2[100];

    // Step 2: Prompt the user to enter the first string
    printf("Enter the first string: ");
    gets(str1);  // Using gets to read the string including spaces

    // Step 3: Prompt the user to enter the second string
    printf("Enter the second string: ");
    gets(str2);  // Using gets to read the string including spaces

    // Step 4: Concatenate the strings using strcat
    strcat(str1, str2);

    // Step 5: Display the concatenated string
    printf("Concatenated string: %s\n", str1);

    return 0;  // Step 6: Return 0 to indicate successful execution
}

C Program Without Using strcat Function

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the two input strings
    char str1[100], str2[100];
    int i, j;

    // Step 2: Prompt the user to enter the first string
    printf("Enter the first string: ");
    gets(str1);  // Using gets to read the string including spaces

    // Step 3: Prompt the user to enter the second string
    printf("Enter the second string: ");
    gets(str2);  // Using gets to read the string including spaces

    // Step 4: Find the length of the first string
    i = 0;
    while (str1[i] != '\0') {
        i++;
    }

    // Step 5: Concatenate the strings manually by appending str2 to str1
    j = 0;
    while (str2[j] != '\0') {
        str1[i] = str2[j];
        i++;
        j++;
    }
    str1[i] = '\0';  // Add the null terminator at the end

    // Step 6: Display the concatenated string
    printf("Concatenated string: %s\n", str1);

    return 0;  // Step 7: Return 0 to indicate successful execution
}

Explanation

Using strcat Function

  • strcat: The strcat function from the string.h library is used to concatenate str2 to the end of str1. This function automatically handles the null terminator (\0).

Without Using strcat Function

  • Manual Concatenation: The program manually concatenates the strings by appending each character from str2 to the end of str1. After appending all characters, it explicitly adds the null terminator to the end of str1.

Steps Explanation:

  1. Declare Variables: The str1 array stores the first input string, and str2 stores the second input string.
  2. Input the Strings: The gets function reads the input strings, including spaces, and stores them in str1 and str2.
  3. Concatenate the Strings: Depending on the approach, either the strcat function or a manual loop concatenates str2 to the end of str1.
  4. Display the Concatenated String: The program displays the concatenated string using the printf function.

Output Example

Example:

Enter the first string: Hello, 
Enter the second string: World!
Concatenated string: Hello, World!

Another Example:

Enter the first string: C Programming 
Enter the second string: is Fun!
Concatenated string: C Programming is Fun!

Conclusion

This C program demonstrates two methods to concatenate two strings: using the strcat function and manually concatenating character by character. It covers basic concepts such as string manipulation, loops, and functions, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top