Saturday 11 November 2017

C Program to read two Strings & Concatenate the Strings


Code:

#include
#include

void main()
{
    char string1[20], string2[20];
    int i, j, pos;

    /*  Initialize the string to NULL values */
    memset(string1, 0, 20);
    memset(string2, 0, 20);

    printf("Enter the first string : ");
    scanf("%s", string1);
    printf("Enter the second string: ");
    scanf("%s", string2);
    printf("First string  = %s\n", string1);
    printf("Second string = %s\n", string2);

    /*  Concate the second string to the end of the first string */
    for (i = 0; string1[i] != '\0'; i++)
    {
        /*  null statement: simply traversing the string1 */
        ;
    }
    pos = i;
    for (j = 0; string2[j] != '\0'; i++)
    {
        string1[i] = string2[j++];
    }
    /*  set the last character of string1 to NULL */
    string1[i] = '\0';
    printf("Concatenated string = %s\n", string1);
}

Output:

Enter the first string : Execute
Enter the second string: Codes
First string  = Execute
Second string = Codes
Concatenated string = ExecuteCodes

More C Programs





100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...