c# - How do I swap two characters in a string variable? -
i have string variable. want swap 2 characters in string word. want randomly swap 2 characters close each other.
this have done: have done in words error.
string word = txtword.text; random rand = new random(); int randomnumber= rand.next(0, word.length); string swappedword = swapcharacters(lastword, randomnumber, randomnumber + 1); private string swapcharacters(string value, int position1, int position2) { char[] array = value.tochararray(); // convert string char array char temp = array[position1]; // temporary copy of character array[position1] = array[position2]; // assign element array[position2] = temp; // assign element return new string(array); // return string }
just change line below:
int randomnumber= rand.next(0, word.length -1 );
let's see if works.
Comments
Post a Comment