Q:

Write a program in C# Sharp to convert a string array to a string

0

Write a program in C# Sharp to convert a string array to a string. 
Test Data :
Input number of strings to store in the array :3
Input 3 strings for the array :
Element[0] : cat
Element[1] : dog
Element[2] : rat
Expected Output:
Here is the string below created with elements of the above array :
cat, dog, rat

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
using System.Linq;
using System.Collections.Generic;
class LinqExercise13
{
	static void Main(string[] args)
		{
		    string[] arr1;
		    int n,i;	    
		    Console.Write("\nLINQ : Convert a string array to a string : "); 
            Console.Write("\n------------------------------------------\n");	
		    Console.Write("Input number of strings to  store in the array :");
		    n= Convert.ToInt32(Console.ReadLine()); 
            arr1=new string[n];      
            Console.Write("Input {0} strings for the array  :\n",n);
                for(i=0;i<n;i++)
                {
                Console.Write("Element[{0}] : ",i);
                arr1[i] = Console.ReadLine();	
                }	
			string newstring = String.Join(", ", arr1
                          .Select(s => s.ToString())
                          .ToArray());
            Console.Write("\nHere is the string below created with elements of the above array  :\n\n");                          
			Console.WriteLine(newstring);      
            Console.Write("\n");
			Console.ReadLine();
		}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now