C program to concatenate two strings

Write a C program to concatenate two different strings into single string. How to concatenate two strings into one without using strcat() library function. Adding two strings into one without using inbuilt library function.

Write a C program to concatenate two strings using inbuilt library function strcat(). How to use strcat() library function.

Example:
Input string1: I love
Input string2: CodeforWin
Output string: I love CodeforWin

Required knowledge

Basic C programming, Loop, String

Concatenation of strings

Concatenation of two strings is the process of joining them together to form a new string. Concatenation basically joins the second string after first string. Concatenation is sometimes also referred as binary addition of strings i.e. + operation.
For example: Codefor + Win = CodeforWin

Program to concatenate two strings

/**
 * C program to concatenate two strings without using inbuilt library function strcat()
 */

#include <stdio.h>

#define MAX_SIZE 100 //Maximum size of the string

int main()
{
    char text1[MAX_SIZE], text2[MAX_SIZE], output[MAX_SIZE];
    int i, j;


    /* Reads the two strings from user */
    printf("Enter first string: ");
    gets(text1);
    printf("Enter second string: ");
    gets(text2);


    /* Copying text1 to output string */
    i=0;
    while(text1[i]!='\0')
    {
        output[i] = text1[i];
        i++;
    }

    /* Copies text2 to output string */
    j = 0;
    while(text2[j]!='\0')
    {
        output[i] = text2[j];
        i++;
        j++;
    }

    //Make sure that the output is NULL terminated
    output[i] = '\0';

    printf("\nFirst string = %s\n", text1);
    printf("Second string = %s\n", text2);
    printf("Final concatenated string = %s\n", output);

    return 0;
}


Note: You can also use inbuilt string library function strcat(str1, str2) to concatenate two strings. Where str2 is appended after str1. This function is present in string.h header file.


Program to concatenate two strings using strcat()

/**
 * C program to concatenate two strings using strcat()
 */

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

#define MAX_SIZE 100 //Maximum size of the string


int main()
{
    char text1[MAX_SIZE], text2[MAX_SIZE], output[MAX_SIZE];
    int i, j;

    /* Reads the two strings from user */
    printf("Enter first string: ");
    gets(text1);
    printf("Enter second string: ");
    gets(text2);


    /* Copies text1 to output */
    strcpy(output, text1);

    /* Concatenates output(i.e. text1) with text2 */
    strcat(output, text2);

    printf("\nFirst string = %s\n", text1);
    printf("Second string = %s\n", text2);
    printf("Final concatenated string = %s\n", output);

    return 0;
}



Output
Enter first string: I love
Enter second string: CodeforWin

First string = I love
Second string = CodeforWin
Final concatenated string = I love CodeforWin

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment