Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

26 February 2012

c program to concatenate two strings without using strcat [ using pointers]

Here I am showing program to Concatenate Strings in 3 different ways:

c program to concatenate two strings without using strcat [ using pointers]

C program to concatenate two strings using strcat().
.For information on Strings and string functions in c

#include <stdio.h>
#include <string.h>
void main(void)
{
   char str1[25],str2[25];
   printf("\nEnter First String:");
   gets(str1);
   printf("\nEnter Second String:");
   gets(str2);
   strcat(str1,str2);                       //using string functions
   printf("\nConcatenated String is %s",str1);
}

C program to concatenate two strings without using strcat()


#include<stdio.h>
void main(void)
{
   char str1[25],str2[25];
   int i=0,j=0;
   printf("\nEnter First String:");
   gets(str1);
   printf("\nEnter Second String:");
   gets(str2);
   while(str1[i]!='\0')
       i++;
   while(str2[j]!='\0')
   {
      str1[i]=str2[j];
      j++;
      i++;
   }
   str1[i]='\0';
   printf("\nConcatenated String is %s",str1);
}

C program to concatenate two strings without using strcat() and using pointers
Here we are creating our own Function to Concatenate two Strings.

#include<stdio.h>
void concat(char* ,char* );
void main(void)
{
   char str1[25],str2[25];
   printf("\nEnter First String:");
   gets(str1);
   printf("\nEnter Second String:");
   gets(str2);
   concat(str1,str2);                                //1
   printf("\nConcatenated String is %s",str1);
}
void concat(char *s1,char *s2)
{
     while(*s1!='\0')                                //2
             s1++
     while(*s2!='\0')                                //3
     {
            *s1=*s2;                                 //4
             s1++;                                         
             s2++;                                         
     }
      *s1='\0';                                      //5
}

Explanation:First program does not require any explanation as it just requires in-built strcat().
Second and Third program have much similar logic.So I am showing the logic behind the third program
Assume two Strings "Science\0" and "Unseen\0".Now I want to concatenate both strings.


Consider statement  //1  there we are Sending two parameters they are the pointers.1st is the base address of String str1 and 2nd is the base address of  String str2.both are collected in character pointers s1 and s2 respectively.So s1 points to S And s2 points to U.


Consider  statement  //2  here we are traversing s1 until s1 points to '\0'.

Now Statement //3 What we want to do now is to copy the contains of str2 at the end of str1.
str1 is pointing to its end.

Statement //4 :Copying of s2 to s1 is done in loop and increments the pointer's untill s2 reaches its end.

 statement //5 :To indicate the termination of s1 we have to add '/0' at the end

Thus we get the Output as ScienceUnseen in Str1.

Step by Step Execution:
String1            String2
|Science\0          |Unseen\0        //   | is indicated as pointer
S|cience\0          |Unseen\0
Sc|ience\0          |Unseen\0
Sci|ence\0          |Unseen\0
Scie|nce\0          |Unseen\0
Scien|ce\0          |Unseen\0
Scienc|e\0          |Unseen\0
Science|\0          |Unseen\0

Now the first loop terminates as '\0' is reached.
The work of second loop is to copy contains of second loop to the end of first loop.

As the pointer is perfectly positioned at the end we have to copy till string 2 terminates

String1                 String2
ScienceU|             U|nseen\0
ScienceUn|           Un|seen\0
ScienceUns|         Uns|een\0
ScienceUnse|        Unse|en\0
ScienceUnsee|      Unsee|n\0
ScienceUnseen|     Unseen|\0
ScienceUnseen\0|        


Similar Programs:

c program to copy one string to another without using strcpy [ using pointers ]

c program to find length of string without using strlen function [ using pointers]

c program to reverse a string without using string functions [ using pointers ]


c program to compare two strings without using strcmp [ using pointers ]


Share your views regarding the given post via the comments section given below.
If you liked the post, please '+1' it & share it on other social networks.
Thank You.

23 comments:

  1. very simple C program to concatenate two strings without using strcat()


    #include
    #include
    void main()
    {
    char a[20],b[20];
    clrscr();
    gets(a);
    gets(b);
    printf("the string is %s%s",a,b);
    getch();
    }

    ReplyDelete
    Replies
    1. hi sathish its nice.. but i wanna store into another variable likec[10]..

      Delete
    2. you are just printing the strings one by one not concatenating them..

      Delete
  2. very simple C program to concatenate two strings without using strcat()


    #include
    #include
    void main()
    {
    char a[20],b[20];
    clrscr();
    gets(a);
    gets(b);
    printf("the string is %s%s",a,b);
    getch();
    }

    ReplyDelete
  3. thanx @sathish but this is just printing two Strings one by one.We just dont wont the output on screen,we wants some string to have contain the concatenated string.for demo assume some application u took the username in one string and then surname
    now u want for some purpose the both string in 1 lets assume for sorting then this will not work
    u have concatenate two strings in real manner
    hope u got my point

    ReplyDelete
  4. Anonymous01 June, 2012

    yaaaaa adninp is correct

    ReplyDelete
  5. thanks for the program.....helped me in finishing my assignment :)

    ReplyDelete
  6. // PORTFOLIO QUESTION 3.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include
    #include
    #include
    #include
    #pragma warning(disable:4996)

    char *mystrcat(char*s1p, char*s2p); // Prototype

    int main(int argc, char * argv)
    {

    char string1[100], string2[100];

    printf("Enter in string 1: ");
    scanf("%s", string1);

    printf("Enter in string 2: ");
    scanf("%s", string2);

    printf("Returned string : %s \n", *mystrcat(string1, string2));

    printf("String1: %s String2: %s\n", string1, string2);


    return 0;

    }

    char *mystrcat(char *s1p,char *s2p)
    {
    while(*s1p!='\0')
    s1p++;

    while(*s2p!='\0'){
    *s1p=*s2p;
    s1p++;
    s2p++;
    }

    *s1p='\0';


    return s1p;
    }



    i need some help with this, it work string 1 contains both string 1 and 2 but in the printf("Returned string : %s \n", *mystrcat(string1, string2)); it prints NULL or nothing, plz help

    ReplyDelete
    Replies
    1. Your first mistake is the way you call function
      Second thing function is returning wrong address rather than base address

      so here it is

      #include
      #include
      #pragma warning(disable:4996)

      char *mystrcat(char*s1p, char*s2p); // Prototype

      int main(int argc, char * argv)
      {

      char string1[100], string2[100];

      printf("Enter in string 1: ");
      scanf("%s", string1);

      printf("Enter in string 2: ");
      scanf("%s", string2);

      printf("Returned string : %s \n", mystrcat(string1, string2)); //look at this again

      printf("String1: %s String2: %s\n", string1, string2);


      return 0;

      }

      char *mystrcat(char *s1p,char *s2p)
      {
      char *sp;
      sp=s1p;
      while(*s1p!='\0')
      s1p++;
      while(*s2p!='\0'){
      *s1p=*s2p;
      s1p++;
      s2p++;
      }
      *s1p='\0';
      return sp;
      }

      Delete
  7. Your family deserves the welcoming and loving space
    of your very house, after all no credit check loans uk if you are not your
    house owner, a debt consolidation loan is not a possibility to suit your needs.

    ReplyDelete
  8. The application process takes just minutes and it is normally approved within 1 hour fast payday loans online
    many associations, unions or perhaps workplaces have their own banks.

    ReplyDelete
  9. Financing a bigger schooling may also have to possess a government-backed education mortgage
    payday loans bad credit lenders will look at the
    co-signer's existing credit.

    ReplyDelete
  10. There are plenty of how you can go about it, like by
    means of mobile phone, personally, or simply on the internet pay day lenders revolving credit, that's made up primarily of credit card debt, increased at an annual rate of 4.

    ReplyDelete
  11. As the pay gap does play a job in numerous lives,
    it isn't the main reason payday loan meticulous planning makes sense, before applying to get a start-up loan.
    my website - payday loan

    ReplyDelete


  12. Неre іs my webѕіtе: Payday Loans Online

    ReplyDelete


  13. Нerе is my ωeb blog :: Payday loans online

    ReplyDelete
  14. Anonymous11 June, 2013

    This comment has been removed by a blog administrator.

    ReplyDelete
  15. Anonymous12 June, 2013

    This comment has been removed by a blog administrator.

    ReplyDelete
  16. Anonymous15 June, 2013

    This comment has been removed by a blog administrator.

    ReplyDelete
  17. Anonymous16 June, 2013

    This comment has been removed by a blog administrator.

    ReplyDelete
  18. @adminps your program will show an error of out of array index!!
    as str[1] is of size 25 only

    ReplyDelete
  19. here is my website www.geeksbeat.com

    ReplyDelete