Program to copy string

Description

To copy a string another variable use library function.

C/C++

/* C Program to copy string*/
//Save it as CopyString.c

#include<stdio.h>
int main(){

    char str[50], newStr[500];

    printf("Enter a string : ");
    scanf("%s",str);

    strcpy(newStr,str);

    printf("The original string : %s",str);
    printf("\nThe new string : %s",newStr);

    return 0;
}
Input:
Enter a string : ramayana

Output:
The original string : ramayana
The new string : ramayana

Java

/* Java Program to copy string*/
//Save it as CopyString.java

import java.io.*;
import java.util.Scanner;

public class CopyString {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String str, newStr;

        System.out.println("Enter a string : ");
        str = scanner.nextLine();
        
        newStr = str;

        System.out.println("The original string : "+str);
        System.out.println("The new string : "+newStr);
    }
}
Input:
Enter a string : 
ramayana

Output:
The original string : ramayana
The new string : ramayana

Related Programs

1) Program to Compare Two Strings
2) Program to Reverse a String
3) Find Substring of a Given String
4) Program to remove vowels from a String
5) Program to Multiply Numbers Present in a String
6) Program to remove given number from a string
7) Program to count Vowels, Consonants, Digits and Whitespaces
8) Program to Add numbers present in a String
9) Program to Replace Spaces With Dots
10) Program to find maximum number of 1s in a substring of a String
Share Me

Leave a Reply