Program to Copy String in Another String
- Write a program to Copy String in Another String in C
- Write a program to Copy String in Another String in C++
- Write a program to Copy String in Another String in Python
- Write a program to Copy String in Another String in PHP
- Write a program to Copy String in Another String in Java
- Write a program to Copy String in Another String in Java Script
- Write a program to Copy String in Another String in C#
Explanation:
Duplicating the content of the source string into the target string is the process of copying a string into another string. This procedure is usually carried out by iterating through each character in the source string and inserting it into the destination string. In languages such as C, this is sometimes done by appending a NULL character to indicate the end of the string.
Logic Steps:
- Initialize Variables:
- Create two strings: one as the source and the other as the target.
- Iterate Through the Source String:
- Loop through each character in the source string.
- Copy Characters:
- Assign each character from the source string to the corresponding position in the target string.
- Null-Terminate the String:
- If necessary (e.g., in C), add a null terminator NULL to the target string.
- Output the Result:
- Display or use the copied string.
Program to Copy String in Another String
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
// Scan the string
printf("Enter a string: ");
fgets(str1, sizeof(str1), stdin);
// Remove newline character from input
str1[strcspn(str1, "\n")] = '\0';
// Copy string using strcpy()
strcpy(str2, str1);
// Print the copied string
printf("Copied string: %s\n", str2);
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string str1, str2;
// Scan the string
std::cout << "Enter a string: ";
std::getline(std::cin, str1);
// Copy string
str2 = str1;
// Print the copied string
std::cout << "Copied string: " << str2 << std::endl;
return 0;
}
str1 = input("Enter a string: ")
# Copy string
str2 = str1
# Print the copied string
print(f"Copied string: {str2}")
<?php // Scan the string echo "Enter a string: "; $str1 = trim(fgets(STDIN)); // Copy string $str2 = $str1; // Print the copied string echo "Copied string: $str2\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 string
System.out.print("Enter a string: ");
String str1 = scanner.nextLine();
// Copy string
String str2 = str1;
// Print the copied string
System.out.println("Copied string: " + str2);
}
}
let str1 = prompt("Enter a string: ");
// Copy string
let str2 = str1;
// Print the copied string
console.log("Copied string: " + str2);
using System;
class Program {
static void Main() {
// Scan the string
Console.Write("Enter a string: ");
string str1 = Console.ReadLine();
// Copy string
string str2 = str1;
// Print the copied string
Console.WriteLine("Copied string: " + str2);
}
}