User input then split string

user6032287

I have looked online for many solutions to my problem. I want to ask the user to input a sentence and write the sentence out one word per line using the split method. I have asked the user to enter a sentence and ran the console but the the sentence keeps appearing on the second line.

namespace Seperation
{
    class Program
    {
        static void Main()
        {

            string temp;
            string sentenceTwo = (" ");

            Console.WriteLine("please enter a sentence");
            temp = Console.ReadLine();
            sentenceTwo = temp;

            string[] split = sentenceTwo.Split(',');
            foreach (string item in split)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();        
        }
    }
}
MDaniyal

You should split the string on space instead of on comma:

namespace Seperation
{
    class Program
    {
        static void Main()
        {
            string temp;
            string sentenceTwo = (" ");

            Console.WriteLine("please enter a sentence");
            temp = Console.ReadLine();
            sentenceTwo = temp;

            string[] split = sentenceTwo.Split(' ');
            foreach (string item in split)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();        
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related