Program to copy string

Description

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
To copy a string another variable use library function.
To copy a string another variable use library function.
To copy a string another variable use library function.

C/C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* 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;
}
/* 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; }
/* 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;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Input:
Enter a string : ramayana
Output:
The original string : ramayana
The new string : ramayana
Input: Enter a string : ramayana Output: The original string : ramayana The new string : ramayana
Input:
Enter a string : ramayana

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

Java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* 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);
}
}
/* 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); } }
/* 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);
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Input:
Enter a string :
ramayana
Output:
The original string : ramayana
The new string : ramayana
Input: Enter a string : ramayana Output: The original string : ramayana The new string : ramayana
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