M
M
Maxim Siomin2020-10-26 16:39:31
C++ / C#
Maxim Siomin, 2020-10-26 16:39:31

How to write a program to sort text into words?

I need to write a program that works like this: the
user input is word1 word2 word3
the program creates a string text
variable then a string[] words
variable and the words variable contains the values:
word1
word2
word3
I wrote this but nothing works

spoiler
using System;
using MyLib;

namespace Shake_your_text
{
    class Program
    {
        private static int getTextLength(string text)
        {
            int sum = 0;

            foreach (char letter in text)
            {
                if (letter == ' ')
                    ++sum;
            }

            return sum;
        }


        private static string[] getWords(string text, int textLength)
        {
            string[] words = new string[textLength];

            for (int x = 0; x < textLength; ++x)
            {
                //c.print("1");
                string word = "";

                for (int y = x; ; ++y)
                {
                    //c.print("2");
                    char letter = text[y];
                    if (letter == ' ')
                        break;
                    else
                        word += letter;
                }


                words[x] = word;
            }






            return words;
        }








        static void Main(string[] args)
        {
            string text = c.input("Paste here your text to shake letters in words in it:", true);
            int textLength = getTextLength(text);
            string[] words = getWords(text, textLength);
            foreach (var word in words) c.print(word, true);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-10-26
@firedragon

Read line
Add to sheet
Sort
List out

D
Developer, 2020-10-26
@samodum

String has a Split method for this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question