Need help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help

I want to switch two characters in any position of the string. i.e input hello output helol or input world output wolrd Any characters by just switch their indexes https://code.sololearn.com/cFCascIEZNVG/?ref=app

2nd Nov 2022, 5:43 AM
Mozzie
Mozzie - avatar
1 Answer
0
The simplest way without use substring etc is get a char array for the word, play with chars and return it as string. // get the word as a char array // (which ia not immutable) char[] a= word.ToCharArray(); // save the first char in a temp var char t= ca[0]; // replace the second first char with the second ca[0]= ca[1]; // replace the second char with the temp var (old first char) ca[1]= t; // return the char array as string return new string(ca);
2nd Nov 2022, 6:13 AM
KrOW
KrOW - avatar