Program to copy a string

Program to copy a string

21 April 2023

21 April 2023

Write a Program to copy a string



If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get a string as input from the user and then copy the string and print it.

 

Input

Hello

Output

Copied string: Hello

 

C Program

#include<stdio.h>

#include<string.h>

int main()

{

    char str1[50];

    char str2[50];

    printf("Enter a string: ");

    fgets(str1,sizeof(str1),stdin);

    strcpy(str2,str1);

    printf("Copied string: %s",str2);

    return 0;

}

 

C++ Program

#include <iostream>

#include<string.h>

using namespace std;

int main()

{

    char str1[50];

    char str2[50];

    cout<<"Enter a string: ";

    fgets(str1,sizeof(str1),stdin);

    strcpy(str2,str1);

    cout<<"Copied string: "<<str2;

    return 0;

}

 

Java

import java.util.Scanner;

public class Main

{

              public static void main(String[] args) {

                  Scanner sc = new Scanner(System.in);

                             System.out.println("Enter a string: ");

                             String str1 = sc.nextLine();

                             String str2 = str1;

                             System.out.println("Copied string: "+str2);

              }

}

 

Python

str1 = input("Enter a string: ")

str2=str1

print("Copied string: ",str2)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023


Related Articles
Program to copy a string

Ask Us Anything !