Electrofriends

Program to concatenate two given string using Pointer

by Ranjith | December 4th, 2008.

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.

Share and Enjoy:
  • Digg
  • Technorati
  • StumbleUpon
  • TwitThis
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Mixx
  • Yahoo! Buzz
  • LinkedIn
  • MySpace
  • FriendFeed
  • NewsVine
  • Netvibes
Similar Posts:

Leave a Reply

Copyright©2009 www.electrofriends.com All Rights Reserved. Powered by Dhyeya