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.

6 Responses to “Program to concatenate two given string using Pointer”

  1. require explanation of this statement in detail: while(*(str1+len)!=’′)

    Reply
  2. 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.

    Reply
  3. 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[ ]…

    Reply
  4. team idea

    Hi,

    Do you want to see your website in Top 10 positions in Google and other major search engine?

    Do you want more targeted visitors on your website?

    If yes,please let me know your website URL and keywords name that you want to optimize so that our team could analyze the keywords, level of competition and could Supply you with our best price to optimize the website.

    Benefits are here

    1-Put your website top 10 in google.com and other major search engines.

    2-Provide per day 300 organic visitors click to your website.

    3-Increase 3000 numbers of facebook likes or other social media improvement as your choice.

    4-Higher traffic and you get visitors to social media platforms.

    5-Long term results in terms of brand awareness.

    6-More sale and Higher return on investment.

    7-You will get More Traffic= Higher Sales.

    8-Increased brand credibility.

    9-Free website analysis report to your website.

    10-Per month provide weekly report with your keywords position.

    We are looking forward for your positive response.

    Thanks

    Reply

Leave a Reply