Program to concatenate two given string using Pointer

This is another example program to concatenate two given strings dynamically using the string pointers. Earlier program explains the way to concatenate two strings by direct method.

Logic : The program is just up gradation of the previous program. Here the program takes two strings to concatenate. Stores that with pointers str1 and str2 pointed to that respectively. The function “stcat” takes 2 pointer argument and keeping one as the reference, traces the other till the end. The EOL (End Of Line) of the first string is taken and then the second string is added at this point, character by character. This inner loop will iterate till the EOL of the second line. After the end of the second string, an EOL will be added. Finally, the result will be displayed.

#include<stdio.h>
#include<conio.h>
void stcat(char *str1, char *str2);
void main()
{
char *str1, *str2;
clrscr();
printf(“\n\n\t ENTER THE FIRST STRING…: “);
gets(str1);
printf(“\n\n\t ENTER THE FIRST STRING…: “);
gets(str2);
stcat(str1,str2);
printf(“\n\t THE CONCATENATED STRING IS…: “);
puts(str1);
getch();
}
void stcat (char *str1, char *str2)
{
int i = 0,len = 0;
while(*(str1+len)!=’\0′)
len++;
while(*(str2+i)!=’\0′)
{
*(str1+len) = *(str2+i);
i++;
len++;
}
*(str1+len) = ‘\0’;
}
Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

3 thoughts on “Program to concatenate two given string using Pointer

  1. The code is very nice and I appreciate the efforts of the programmers who indulge themselves in programming for applications engineers and software engineers to be able to continue sending feedbacks about the latest arrivals of thoughts upon the professional associations requests for creating something relevant for their needs and even for them to continue their works and telecasts to other companies and enterprises in international countries for further bright opportunities for them and their families and children.

  2. this program will show RUNTIME ERROR because *str1 points to a constant string and you can not change its value with statement *(str1+len) = *(str2+i); RATHER declare char str1[ ],str2[ ]…

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in