C# Program to swap adjacent characters of a string

Hi Friends, this article for swap adjacent characters of a string in C# Language.

Code explanation
if you debug the program then control goes to Main Method then it starts processing bloc of Main method.
After that control goes to Console class and write method and print “Enter Any String : ” into output screen.
after that String str = Console.ReadLine() . whatever string we enter that accept inside str. suppose if i enter
Amazon so str = Amazon. next control goes to swap method swap(str) or swap(Amazon) here after compiler search where is
swap method. so control go up “static void swap(string st) ” st = Amazon. now it starts processing swap methods and next line
char[] arr =st.ToCharArray(). by using ToCharArray it converts string to char and and store arr of index.
after that i created StringBuilder object s by using new method. next line for loop , i =0 condition 0 <=5 because total
characters 6 -1=5. inside for loop if statement there when condition true then object of stringbuilder class s starts adding
characters with increasing arr index. suppose if condition not true then simple it will add characters into s and it
starts increasing i value whenever condition true. if condition false then s.ToString() , s converts into string and output
display ” After Swapping : mAzano.

Post Author: adama