I am new to C# and trying to figure out how I could do this.
So, what I am trying to do right now is to make a textbox out of text like this.
If I have a sentence saying "Hello World. I have a problem. Can you help me?", I want to randomly pick one word and let the user fill it in. Could you please help me how to randomize the pick and make a textbox?
Thanks in advance for your help! :)
PS. I posted a pic for better understanding
Example picture of what I want to do
Here is a way to get a random word of a sentence ...
You might split the sentence (tb.Text) into a string [] of words by using string.Split(' '). Then get a random number between '0' and .Length of your words.
Then pick the random substring.
// split the string into substrings seperated by a blank ' ' ...
var strArrWords = tb.Text.Split(' ');
// get the number of words ...
var numWords = strArrWords.Length;
// get a random number between '0' and numWords-1
Random rnd = new Random();
var number = rnd.Next(0, numWords - 1);
// finally get your random word ...
var word = tb.Text.Split(' ')[number];
or the same in 2 lines ...
Random rnd = new Random();
var word = tb.Text.Split(' ')[rnd.Next(0, tb.Text.Split(' ').Length - 1)];
To visualize this, you can maybee open another textbox displaying your word, the user can edit it. Then you replace the word in the other box, depends ...
I hope that helps :-)
Related
I am a beginner in programming. And now I'm facing a task where I can't get any further. Probably it is relatively easy to solve.
This is what I want to do: I read out a .txt file and there are several lines of content.
Example what is in the .txt file:
text1,text2,text3
text1,text2,
text1,text2,text3,text4
I'm now ready to find the right line and use it. Then I want to split the line and assign each text to its own string.
I can do this if I know that this line have 4 words. But what if I don't know how many words this line have.
For example if I want to assign 5 strings but there are only 4 arrays in the column I get an error.
My program currently looks like this:
string reader = "text1,text2,text3,text4";
string[] words = reader.Split(',');
string word1 = words[0].ToString();
string word2 = words[1].ToString();
string word3 = words[2].ToString();
string word4 = words[3].ToString();
textBox1.Text = word3;
My goal is to find out how many words are in the string. And then pass each word to a separate string.
Thank you in advance
To get the length of the Array, you can easily use .Length
In your example, you just write
int arraylength = words.Length;
I don't understand, why do you want to create a new String for every value of the string-array? You can just use them in the array.
In your example you always user .ToString(), this isn't necessary because you already have a string.
An array is just multiple variables (in your example strings) which are connected to another.
I doubt if you want separated local variables like word1, word2 etc. To see why, let's
bring the idea to the point of absurdity. Imagine, that we have a small narration with 1234
words only. Do we really want to create word1, word2, ..., word1234 local variables?
So, let's stick to a single words array only:
string[] words = reader.Split(',');
Now, you can easily get array Length (i.e. number of items):
textBoxCount.Text = $"We have {words.Length} words in total";
Or get N-th word (let N be one based) from the words array:
string wordN = array.Length >= N ? array[N - 1] : "SomeDefaultValue";
In your case (3d word) it can be
// either 3d word or an empty string (when we have just two less words)
textBox1.Text = array.Length >= 3 ? array[3 - 1] : "";
Technically, you can use Linq and query the reader string:
using System.Linq;
...
// 3d word or empty string
textBox1.Text = reader.Split(',').ElementAtOrDefault(3 - 1) ?? "";
But Linq seems to be overshot here.
I am writing a program where I have a set of number 123456789 and words ABCDEFGHI. Now if a user enters any number its equivalent letter should show up in the result. Can someone guide me on how to approach this question.
For EX: user entry of 1352 should result in ACEB
Welcome here, your question is too 'easy' to become a question. And at lease you should show up what you have done.
But I will give you a shot.
I have wrote simple method for solve your question.
Sandbox to run this online
//Your code goes here
Console.WriteLine("Hello, world!");
//predifine your sets
var inputSet = new List<char> {'1','2','3','4','5','6','7','8','9','0'};
var outputSet = new List<char>{'A','B','C','D','E','F','G','H','I','J'};
//lets parse
Console.WriteLine(new string("1352".Select(x=>outputSet[inputSet.IndexOf(x)]).ToArray()));
Console.WriteLine(new string("199466856".Select(x=>outputSet[inputSet.IndexOf(x)]).ToArray()));
Console.WriteLine(new string("111222333444".Select(x=>outputSet[inputSet.IndexOf(x)]).ToArray()));
Result:
Hello, world!
ACEB
AIIDFFHEF
AAABBBCCCDDD
Edit:
Explain how it works.
"1352".Select(x) To select chars one by one in the string and store in x.
inputSet.IndexOf(x) To find position of x in inputSet
outputSet[int] To get value by given position from found position in inputSet recenly
new string(char array) Instantiate a new string by given char array.
Lets say I have a String Array full of items such as:
string[] letters = new string[4] {"A1","B1","C1","D1"};
Later, I want to set the contents of a textbox to the first value in the array:
Letter.Content = letters[0];
Is there a way to 'clip' the number out of the String in the Array? For example, in my above code, currently the Letter textbox would be set to 'A1'. What I want however is to set it to just 'A'.
Depends on if the strings's length is always two and the digit is at the second position. Then it's simple:
Letter.Content = letters[0][0];
If you don't know the length but you want to take all letters from the left until there is a non-letter you could use string.Concat + LINQ:
Letter.Content = string.Concat(letters[0].TakeWhile(Char.IsLetter));
or you could do it the old fashion way using SubString Method
Letter.Content = letters[0].Substring(0,1);
I split the input paragraph by . and store it in an array like this:
string[] totalSentences = inputPara.Split('.')
then the function below is called which calculates total number of Words from each sentence like this:
public void initParaMatrix()
{
int size = 0;
for (int i = 0; i < totalSentences.Length; i++)
{
string[] words = totalSentences[i].Split();
size = size + words.Length;
//rest of the logic here...
}
matrixSize = size;
paraMatrix = new string[matrixSize, matrixSize];
}
paraMatrix is a 2D matrix equal to length of all words which I need to make in my logic.
The problem here is when I input only one sentence which has 5 words, the size variable gets the value 7. I tried the debugger and I was getting total of 2 sentences instead of 1.
Sentence 1. "Our present ideas about motion." > this is actual sentence which have only 5 words
Sentence 2. " " > this is the exact second sentence I'm getting.
Here is the screenshot:
Why I'm getting two sentences here and how is size getting value 7?
This makes perfect sense. If the second sentence has nothing but a " ", and you split along the " ", then you'll have two empty strings as a result. The easiest thing to do here is change the line you do the split, and add a trim:
string[] words = totalSentences[i].Trim().Split();
I don't know what version of Split that you're using since it accepts no parameters, but if you use String.Split you can set the second parameter so that empty entries are automatically removed by using the option StringSplitOptions.RemoveEmptyEntries.
You're not resetting the size integer to zero. So that's why you get 7 for the second sentence.
For the second sentence, which is a space, try inputPara.Trim() which should remove the space at the end of the string.
I have been asked to make a game at work where I have to change the letters to dashes. I have been told to use a multi dimensional array but I get the full word out of a text file. This is the code i have so far
Console.WriteLine("Enter file path:");
string filePath= Console.ReadLine();
// read all the lines from the file
string[] lines = File.ReadAllLines(readFilePath);
// get a random number between 0 and less than the number of lines in the file
Random rand = new Random();
int chosenLineIndex = rand.Next(lines.Length);
// choose the line at the line number
string chosenLine = lines[chosenLineIndex];
// write the line to the Console
Console.WriteLine(chosenLine);
// make an array containing the only the chosen line
string[] chosenLines = new string[] { chosenLine };
So does anyone know how to write the letters separately to the array instead of the full word?
I believe you are looking for String.ToCharArray
This will give you an array that contains each character separately as a string.
string[] chosenLines = chosenLine.Select(x => x.ToString()).ToArray();
You can also use a char[] array instead.Then you won't need Select, you can just use String.ToCharArray method.