w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Copy one string into another string

C# Sharp String: Exercise-8 with Solution

Write a program in C# Sharp to copy one string to another string.

C# Sharp Exercises: Copy one string into another string

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise8  
{  
    public static void Main() 
{
    string str1;
    int  i,l;

      Console.Write("\n\nCopy one string into another string :\n");
      Console.Write("-----------------------------------------\n"); 	
      Console.Write("Input the string : ");
      str1 = Console.ReadLine();
      
      l=str1.Length;
      string[] str2=new string[l];

    /* Copies string1 to string2 character by character */
    i=0;
    while(i<l)
    {
        string tmp=str1[i].ToString();
        str2[i] = tmp;
        i++;
    }
   Console.Write("\nThe First string is : {0}\n", str1);
   Console.Write("The Second string is : {0}\n", string.Join("",str2));
   Console.Write("Number of characters copied : {0}\n\n", i);
	}
}

Sample Output:

Copy one string into another string :                                  
-----------------------------------------                              
Input the string : w3resource.com                                      
                                                                       
The First string is : w3resource.com                                   
The Second string is : w3resource.com                                  
Number of characters copied : 14

Flowchart:

Flowchart: Copy one string into another string

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to count a total number of alphabets, digits and special characters in a string.
Next: Write a program in C# Sharp to count a total number of vowel or consonant in a string.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming