String concatenation in C is the process of joining two or more strings into a single string. This involves taking characters from one string and appending them to the end of another string until the null terminator ('\0') of the first string is reached.

Implementation of String Concatenation Program

Here’s a simple implementation of a program that concatenates two strings in C:

#include <stdio.h> // Function to concatenate two strings void stringConcatenate(char *dest, const char *src) { // Move dest to the end of the current string while (*dest != '\0') { dest++; // Find the end of the destination string } // Append the source string to the destination while (*src != '\0') { *dest = *src; // Copy each character from source to destination dest++; // Move to the next position in destination src++; // Move to the next position in source } *dest = '\0'; // Null-terminate the concatenated string } int main() { char str1[100]; // First string char str2[100]; // Second string // Ask user for input printf("Enter first string: "); fgets(str1, sizeof(str1), stdin); // Read first string including spaces str1[strcspn(str1, "\n")] = 0; // Remove newline character if present printf("Enter second string: "); fgets(str2, sizeof(str2), stdin); // Read second string including spaces str2[strcspn(str2, "\n")] = 0; // Remove newline character if present // Concatenate str2 to str1 stringConcatenate(str1, str2); // Display the result printf("Concatenated string: %s\n", str1); return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This header file is included for input and output functions.
  2. Function Definition:

    • void stringConcatenate(char *dest, const char *src): This function concatenates the source string to the destination string.
    • Parameters:
      • char *dest: Pointer to the destination string, which will be modified to hold the concatenated result.
      • const char *src: Pointer to the source string to be appended.
  3. Finding the End of the Destination String:

    • The first while loop (while (*dest != '\0')) moves the dest pointer to the end of the current string by incrementing it until it reaches the null character.
  4. Appending the Source String:

    • The second while loop (while (*src != '\0')) copies characters from the source string to the destination string.
    • Each character from src is copied to the current position in dest, and both pointers are incremented.
  5. Null Terminator:

    • After copying all characters from the source string, a null character is added to the end of the destination string to properly terminate it.
  6. Main Function:

    • Two character arrays, str1 and str2, are declared to hold the input strings.
    • The user is prompted to enter both strings using fgets, which allows spaces in the input.
    • The strcspn function is used to remove any newline character from the input strings.
    • The stringConcatenate function is called to concatenate str2 to str1.
    • Finally, the concatenated string is displayed.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc string_concatenate.c -o string_concatenate
  2. Execute the Program:

    ./string_concatenate
  3. Input Data: Enter two strings when prompted.

Example Input/Output

Input:

Enter first string: Hello Enter second string: World!

Output:

Concatenated string: HelloWorld!

Conclusion

The string concatenation program in C demonstrates how to manually join two strings together. Understanding this process is essential for manipulating strings in C, especially since C does not provide built-in operators for string concatenation like higher-level languages. This program reinforces the concepts of pointers, character manipulation, and memory handling in C programming.