Introduction:
This C program concatenates two strings provided by the user. It reads two separate strings and combines them into a single string while demonstrating fundamental string manipulation techniques. String concatenation is a common operation in programming, often used in data formatting, user input processing, and building dynamic messages.

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

int main() {
    char str1[100], str2[100], result[200];

    printf("Enter the first string: ");
    fgets(str1, sizeof(str1), stdin);
    str1[strcspn(str1, "\n")] = 0; // Remove newline character

    printf("Enter the second string: ");
    fgets(str2, sizeof(str2), stdin);
    str2[strcspn(str2, "\n")] = 0; // Remove newline character

    strcpy(result, str1); // Initialize result with the first string
    strcat(result, str2); // Concatenate the second string

    printf("Concatenated string: %s\n", result);
    return 0;
}

  1. Include Header Files:
    • #include <stdio.h>: This header file is included for standard input/output functions like printf and fgets.
    • #include <string.h>: This header file is included for string manipulation functions like strcat and strcpy.
  2. Declare String Arrays:
    • char str1[100], str2[100], result[200];: Three character arrays are declared:
      • str1: To hold the first input string.
      • str2: To hold the second input string.
      • result: To hold the concatenated result of both strings. It is sized to accommodate both strings plus a null terminator.
  3. Input the First String:
    • printf("Enter the first string: ");: Prompts the user for input.
    • fgets(str1, sizeof(str1), stdin);: Reads a line of text, including spaces, and stores it in str1.
    • str1[strcspn(str1, "\n")] = 0;: This line removes the newline character from the input. strcspn finds the length of the string until the newline, and this is set to 0 to terminate the string there.
  4. Input the Second String:
    • Similar to the first string, the program prompts for and reads the second string, storing it in str2 and removing any newline character.
  5. Copy the First String to Result:
    • strcpy(result, str1);: Copies the contents of str1 into result. This initializes result with the first string.
  6. Concatenate the Second String:
    • strcat(result, str2);: Appends the contents of str2 to result. After this operation, result contains both str1 and str2.
  7. Display the Concatenated Result:
    • printf("Concatenated string: %s\n", result);: Prints the concatenated string to the console.
  8. Return Statement:
    • return 0;: Indicates successful completion of the program.

Input/Output Block:

Input:

  • The user is prompted to enter two strings to be concatenated.

Example Input:

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

Output:

  • The program outputs the combined result of the two strings.

Example Output:

Concatenated string: HelloWorld!

Conclusion:

The string concatenation program effectively demonstrates how to combine two strings in C using manual string manipulation techniques. By utilizing pointer arithmetic and loop constructs, the program illustrates a clear method for appending one string to another. This capability is essential in various programming tasks, such as building user interfaces, generating dynamic content, and processing textual data. Overall, the program serves as a practical example of string handling in C, reinforcing fundamental programming concepts and techniques.

Leave a Reply

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

Verified by MonsterInsights