20130428

CPP program for concatenating two strings without using inbuilt library function strcat


How to concatenate two strings using pointer or array. CPP program for concatenating two strings without using inbuilt library function strcat. How to concatenate two strings in cpp/C++/C without using inbuilt in function strcat.Program to concatenate two strings using pointer.

#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20];
printf("Enter a String:\n");
scanf("%s",&arr1);


char arr2[20];
scanf("%s",&arr2);

printf("You entered:%s and %s\n",arr1,arr2);

int i=0,j=0,length=0;

for(i=0;arr1[i]!='\0';i++)
{
    length++;
}

for(i=length,j=0;arr2[j]!='\0';i++,j++)
{
    arr1[i]=arr2[j];
}
arr1[i]='\0';

printf("Concated string is:%s",arr1);
    return 0;
}
/*
Enter a String:
Hello
India
You entered:Hello and India
Concated string is:HelloIndia
*/

Previous                             Home                               Next

1 comment:

  1. SYNTAX for strcat the inbuilt strcat library function to concatenate two strings in cpp.
    strcat(destination,source);
    strcate function takes two strings destination string to which the source string gets appended.
    sample program:

    #include
    #include
    int main()
    {
    char source[]="howto";
    char destination[]="cppdotcom";
    strcat(destination,source);
    cout<<"The new appended string is:"<<dest<<endl;
    }
    /*It outputs into:
    The new appended string is: howtocppdotcom*/

    ReplyDelete