Program to Concat Two Strings
- Write a program to Concat Two Strings in C
- Write a program to Concat Two Strings in C++
- Write a program to Concat Two Strings in Python
- Write a program to Concat Two Strings in PHP
- Write a program to Concat Two Strings in Java
- Write a program to Concat Two Strings in Java Script
- Write a program to Concat Two Strings in C#
Explanation:
When two strings are concatenated, the contents of the second string are appended to the end of the first string. The two strings are combined into one via this technique.
Logic Steps:
- Initialize Two Strings:
- Define the source strings to be concatenated.
- Determine the Result’s Length:
- Ensure the target string has enough space to hold the combined content (important in languages like C).
- Copy the First String:
- Copy all characters from the first string into the target string.
- Append the Second String:
- Add all characters from the second string at the end of the first string in the target.
- Null-Terminate the String:
- In languages like C, append the null terminator NULL to mark the end of the combined string.
- Output the Result:
- Display or use the concatenated string.
Program to Concat Two Strings
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <string.h> int main() { char str1[100], str2[100]; // Scan the two strings printf("Enter first string: "); fgets(str1, sizeof(str1), stdin); printf("Enter second string: "); fgets(str2, sizeof(str2), stdin); // Remove newline characters if present str1[strcspn(str1, "\n")] = '\0'; str2[strcspn(str2, "\n")] = '\0'; // Concatenate strings using strcat() strcat(str1, str2); // Print the concatenated string printf("Concatenated string: %s\n", str1); return 0; }
#include <iostream> #include <string> int main() { std::string str1, str2; // Scan the two strings std::cout << "Enter first string: "; std::getline(std::cin, str1); std::cout << "Enter second string: "; std::getline(std::cin, str2); // Concatenate strings using + operator std::string result = str1 + str2; // Print the concatenated string std::cout << "Concatenated string: " << result << std::endl; return 0; }
str1 = input("Enter first string: ") str2 = input("Enter second string: ") # Concatenate strings using + operator result = str1 + str2 # Print the concatenated string print(f"Concatenated string: {result}")
<?php // Scan the two strings echo "Enter first string: "; $str1 = trim(fgets(STDIN)); echo "Enter second string: "; $str2 = trim(fgets(STDIN)); // Concatenate strings using . operator $result = $str1 . $str2; // Print the concatenated string echo "Concatenated string: $result\n"; ?>
import java.util.Scanner; public class Main { public static void main(String[] args) { // Initialize scanner object for user input Scanner scanner = new Scanner(System.in); // Scan the two strings System.out.print("Enter first string: "); String str1 = scanner.nextLine(); System.out.print("Enter second string: "); String str2 = scanner.nextLine(); // Concatenate strings using + operator String result = str1 + str2; // Print the concatenated string System.out.println("Concatenated string: " + result); } }
let str1 = prompt("Enter first string: "); let str2 = prompt("Enter second string: "); // Concatenate strings using + operator let result = str1 + str2; // Print the concatenated string console.log("Concatenated string: " + result);
using System; class Program { static void Main() { // Scan the two strings Console.Write("Enter first string: "); string str1 = Console.ReadLine(); Console.Write("Enter second string: "); string str2 = Console.ReadLine(); // Concatenate strings using + operator string result = str1 + str2; // Print the concatenated string Console.WriteLine("Concatenated string: " + result); } }