Concatenate Strings Using Pointer

CCBeginner
Practice Now

Introduction

In C programming, we can concatenate two strings using pointer by pointing the base address of the two strings to a char pointer variable and incrementing the pointer to the end of the first string and then copying the characters of the second string to the end of the first.

In this lab, you will learn how to concatenate two strings using pointer in C programming language.

Create a new file

Firstly, open your text editor and create a new file named main.c in the ~/project/ directory.

Include Header Files

In this step, include the necessary header files in the program which are stdio.h and stdlib.h.

#include <stdio.h>
#include <stdlib.h>

Define the main() function

The main() function is used to define the body of the C program.

int main(){
  // TODO: Write code here
  return 0;
}

Declare Variables

In this step, declare two character arrays aa and bb with a size of 100 bytes each.

char aa[100], bb[100];

Input Strings

In this step, the user will provide the values for the two strings aa and bb and store them using the gets() function.

printf("Enter the first string: ");
gets(aa);

printf("Enter the second string to be concatenated: ");
gets(bb);

Concatenate Strings

In this step, we will define two character pointers a and b and start concatenating the second string bb with the first one aa.

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

while(*a){
  a++;
}

while(*b){
  *a = *b;
  b++;
  a++;
}
*a = '\0';
  • *a is pointing to the next memory location as long as it does not point to the end of the first string aa. This is done using the expression while(*a).
  • Here, a is incremented to the next memory location until it reaches the end of the first string aa.
  • *b is pointing to the next memory location until it does not point to the end of the second string bb. This is done using the expression while(*b).
  • For every memory location, a is assigned to the value pointed by b and then both a and b are incremented. This is done using the expression *a = *b; b++; a++;
  • The '\0' represents the end of the string.

Display Concatenated String

In this step, display the concatenated string after the completion of the concatenation process.

printf("\nThe string after concatenation is: %s ", aa);

Compile and Run

In this step, compile and run the main.c file using the following commands:

gcc main.c -o output
./output

Full code

Below is the full code for the Concatenate Strings Using Pointer program:

#include <stdio.h>
#include <stdlib.h>

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

  printf("Enter the first string: ");
  gets(aa);

  printf("Enter the second string to be concatenated: ");
  gets(bb);

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

  while(*a){
    a++;
  }

  while(*b){
    *a = *b;
    b++;
    a++;
  }
  *a = '\0';

  printf("\nThe string after concatenation is: %s ", aa);

  return 0;
}

Summary

In this lab, you learned how to concatenate two strings using pointer in C programming language. We did this by pointing the base address of the two strings to a char pointer variable and incrementing the pointer to the end of the first string and then copying the characters of the second string to the end of the first. Remember to compile and run the code to check its functionality.

Other C Tutorials you may like