Program to Concatenate Strings using Pointer

0 June 15, 2021

In this example, We will learn a simple program to Concatenate Strings using Pointer. Before that, You must have knowledge of the following topics.

Download Application: Learn C Programming with Live Compiler

Algorithm

This is the algorithm of this program

Step 1: Start
Step 2: Initialize string variables aa and bb
Step 3: Takes string input from user and store in aa and bb
Step 4: Declare Pointer variables a and b
Step 5: a = aa and b = bb
step 6: REPEAT this step until pointer a is NULL
        Increase a by 1
step 7: REPEAT this step until pointer b is NULL
        *a = *b
        Increase b by 1
        Increase a by 1
Step 8: *a = \'\\0\'
Step 9: Print aa
Step 10: End

Flowchart

This is the flowchart of this program

Flowchart : Program to Concatenate Strings using Pointer

CODE: Concatenate Strings using Pointer

Now, We will see the main code to concatenate two strings using pointers.

#include <stdio.h>

int main()
{
    
    char aa[100], bb[100];

    printf("\nEnter string: ");
    gets(aa);

    printf("\nEnter the string to be concatenated: ");
    gets(bb);

    char *a = aa;
    char *b = bb;

    // pointing to the end of the 1st string
    while(*a)
    {
        a++;
    }

    while(*b)
    {
        *a = *b;
        b++;
        a++;
    }
    
    *a = '\0';
    printf("\nThe string after concatenation is: %s ", aa);
    
    return 0;
}

The output of above program is

Enter string: Hello 

Enter the string to be concatenated: World

The string after concatenation is: Hello World
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

!

Ads

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System