Aksword
❔ Permut array's values
Hello, is there a convenient way to swap array values in their array, for example transforming a {1,2,3,4} into a {1,4,2,3} I dunno. Because when I think of this I believe that a need to create a copy of the first array as a reference before swapping the values, but maybe there is a better way.
6 replies
Get the first letters of a string
Hello,
Let's say that I have the string "tyranitar". With string[X], I can get the letter that I want, but is there a method or a fast way to get the first X chars of a string ? For exemple if I want "tyrani", how can I ask to substract from [0] to [6] ?
Thanks
17 replies
How can I modify array values in a for loop ? [Answered]
Hello, I wanted to do a piece of code that replaces half of the words inserted by the user (in a random order) into "train", for example. So I did this :
Random numbergen = new Random();
string answ = Console.ReadLine();
string[] words = answ.Split(' ');
int length = words.Length;
for (int i = 1; i < length/2; i ++)
{
determ :
string target = words[numbergen.Next(0, length - 1)];
string replacer = "train";
if (target == replacer)
{
goto determ;
}
else
{
target = replacer;
}
}
for (int i = 0; i<= length - 1; i ++)
{
Console.Write(words[i] + " ");
}
But when I check what the console print, I see that the words aren't replaced, and then I discovered that when you modify an array in a for loop, it's only modified IN the for loop. So does anyone has a clue to change arrays INSIDE of a for loop and be able to access them OUTSIDE of the loop ?
(Thx 😄 )
7 replies