Q. Write an algorithm and program to concatenate two strings without using library function.

Solution : Given two Strings, the task is to concatenate the two Strings without using another string and without using concat function.

Concatenate :- Concatenation means combining two strings together.
Example :-
Input: str1 = "letsfind", str2 = "course"
Output: letsfindcourse

Algorithm For Concatenate Two Strings

//Algorithm For Concatenate Two Strings Without Using Library Function.
START
Step 1 ->  Take two string as str1 and str2.
Step 2 ->  Move to the last position of first string let it be i.
Step 3 ->  Now Loop over second string with j = 0
Assign the character str1[i] = str2[j]
Increment i and j and repeat till the last element of j.
End Loop 
Step 3 ->  Print str1.                
STOP


Program For Concatenate Two Strings

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program For Concatenate Two Strings Without Using Concat Function.
    #include <stdio.h>
    int main()
    {
    char str1[100] = "hello ", str2[] = "world!";
    int i, j;
    for (i = 0; str1[i] != '\0'; ++i);
    for (j = 0; str2[j] != '\0'; ++j, ++i) {
    str1[i] = str2[j];
    }
    str1[i] = '\0';
    printf("After Concatenation Of Two String: ");
    puts(str1);
    return 0;
    }
    
    //C++ Program For Concatenate Two Strings Without Using Concat Function.
    #include <iostream>
    using namespace std; 
    int main() 
    { 
    char str1[100] = "hello ", str2[] = "world!";
    int i, j;
    for (i = 0; str1[i] != '\0'; ++i);
    for (j = 0; str2[j] != '\0'; ++j, ++i) {
    str1[i] = str2[j];
    }
    str1[i] = '\0';
    cout<<"After Concatenation Of Two String: ";
    cout<<str1;
    return 0; 
    }
    
    //Java Program For Concatenate Two Strings Without Using Concat Function.
    public class LFC
    {
    public static void main(String[] args) {
    String str1 = "hello ", str2 = "world!";
    int i, j;
    str1=str1 + "" + str2;
    System.out.println("After Concatenation Of Two String: "+str1);
    }
    }
    
    //Python Program For Concatenate Two Strings Without Using Concat Function.
    str1="hello "
    str2="world"
    str1=str1 + "" + str2;
    print("After Concatenation Of Two String: ",str1) 
    
    //C# Program For Concatenate Two Strings Without Using Concat Function.
    using System;
    class LFC {
    static void Main() {
    string str1="hello ",str2="world!";
    str1=str1+""+str2;
    Console.WriteLine("After Concatenation Of Two String: "+str1);
    }
    }
    
    //PHP Program For Concatenate Two Strings Without Using Concat Function.
    <?php
    $str1="hello ";
    $str2="world!";
    $str1=$str1.$str2;
    echo "After Concatenation Of Two String: $str1";
    ?>
    //C Program For Concatenate Two Strings Without Using Concat Function.
    #include <stdio.h>
    int main()
    {
    char str1[100] = "hello ", str2[] = "world!";
    int i, j;
    for (i = 0; str1[i] != '\0'; ++i);
    for (j = 0; str2[j] != '\0'; ++j, ++i) {
    str1[i] = str2[j];
    }
    str1[i] = '\0';
    printf("After Concatenation Of Two String: ");
    puts(str1);
    return 0;
    }
    

    Output

    After Concatenation Of Two String: hello world!
    

    Recommended Programs

       Program of binary search.
       Program to print Lower triangular matrix of an array.