Convert string to char array in C#

How to convert a string to a char array using C#. The ToCharArray method of the string class converts a string to a character array

The following code snippet creates a string into a char array.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace StringtoCharArray
{
class Program
{
static void Main(string[] args)
{
string s;
Console.WriteLine(“enter string:“);
s = Console.ReadLine();
Console.WriteLine(s);
char[] ch = new char[s.Length];
ch = s.ToCharArray();
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(ch[i]);
}}
}
}

Now Run Program and check output

2 Comments

Leave a Reply

Your email address will not be published.