Answer the question
In order to leave comments, you need to log in
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question