Untitled

 avatar
unknown
plain_text
a month ago
1.0 kB
2
Indexable
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string sentence = "This is a simple sentence";
        List<string> words = new List<string>();

        string currentWord = "";
        foreach (char c in sentence)
        {
            if (c == ' ')
            {
                if (currentWord != "")
                {
                    words.Add(currentWord); // Add the current word to the list
                    currentWord = "";      // Reset currentWord for the next word
                }
            }
            else
            {
                currentWord += c; // Build the current word
            }
        }

        // Add the last word if it exists
        if (currentWord != "")
        {
            words.Add(currentWord);
        }

        // Convert List to Array
        string[] wordArray = words.ToArray();

        // Print the array
        foreach (string word in wordArray)
        {
            Console.WriteLine(word);
        }
    }
}
Editor is loading...
Leave a Comment